V magic stuff

Published .. 28-06-2024
Type ....... article
Tags ....... v

I'm used to Go and i must admit that i find the magic in Go a bit distasteful.

By magic i refer to things that are available but not defined, things that is not part of the language but still implementes somehow in the V compiler.

Magic:

// where does that magic err variable come from?
if x:=some_func() or { println(err) }

By the way its probertly mentally should be read like this:

if x := (some_func() or { 0 })

so the expression returns the value of some_func() unless its and error in which case the result if the error { } part is returned. Then one of these values are assigned to x.

  mut e := Entry{
    title: title
    slug: slug
    created_at: time.now()
  }

  // some magic making it possible to use SQL inside V
  sql app.db {
    insert e into Entry
  } or { return ctx.text('error') }

In Lisp you would be able to make such a DSL in a library. But in V it's magiv because its done on a compiler level and you cannot make your own using the V language.

And then there are sorting:

mut number:=[1,7,4,3]
number.sort(a>b) // a and b just magically appears.

If you dislike, you can use sort_with_compare which take a function that compares two items.

Again it seems the event though V is not a clean as Go, it certainly strives to be a language that gives the programmer shortcuts where appropriate.