V: rethink simplified implicit struct arguments

Created on 18 May 2020  路  5Comments  路  Source: vlang/v

It seems V introduced a new syntax for applying a single struct to a function:

struct Point
{
   x int
   y int
}

fn f(p Point) { }

f(x: 0, y: 0)

Some people have argued that this looks like it is giving named arguments to the function. It also introduces a new, foreign syntax that is specific to applying a single struct to a function.

I would personally propose allowing for the struct name to be ellided when the struct type is inferrable, so you鈥檇 still need to write the curly braces:

f({x: 0, y: 0})

I feel like this is clean enough to look at, while also being explicit enough to be clear that it鈥檚 creating a struct, as well as flexible enough to allow multiple arguments.

Discussion

Most helpful comment

f({x: 0, y: 0})

As someone who is quite new to V, I find this unclear too. My first impression of the code above was the function gets a Map as an argument. In that regard, the originally proposed f(x: 0, y: 0) looks no worse to me.
And if there is a rule to remember anyway, the less typing variant is preferable.

All 5 comments

Code f({x: 0, y: 0}) is better than f(x: 0, y: 0), you know it's a struct, but the whole thing is still bad. We need a good one, not the less worse one.

When you read this function call, you understand the argument is a struct, but which/what struct?
You must jump to function (f()) declaration to figure it out.

V UI module is using this syntax, but that doesn't mean this syntax is good or better, it just means Alex prefers this in V UI module. If V is just a personal project (developed by developer himself and used by himself), nothing is wrong to use whatever syntax the developer like. But if you promote V publicly and wish others using it, let's get it right. if V UI is wrong, admit it and let's fix V UI instead of messing up V.

Also, some code snippets in V for example.

1) https://github.com/vlang/v/blob/master/examples/database/pg/customer.v#L13

db := pg.connect(pg.Config{
        host: 'localhost' //'127.0.0.1'
        user: 'postgres'
        dbname: 'customerdb'
    }) or { ... }

You don't mind using pg.Config{...} here.

2) https://github.com/vlang/v/blob/7f4cf08516c04a9f48f18cc2ca193c022cb32dfd/examples/tetris/tetris.v#L36

const (
    text_cfg = gx.TextCfg{
        align:gx.align_left
        size:TextSize
        color:gx.rgb(0, 0, 0)
    }
        ...
)
...
        g.ft.draw_text(1, 3, g.score.str(), text_cfg)

You don't mind declaring a const with the "traditional" struct syntax first then use the const later as function argument (although not only one argument).

So the question is, why? Why it's ok here and there, but not in V UI module? Which part is wrong? Where's the god damn "one-way" philosophy?

f({x: 0, y: 0})

As someone who is quite new to V, I find this unclear too. My first impression of the code above was the function gets a Map as an argument. In that regard, the originally proposed f(x: 0, y: 0) looks no worse to me.
And if there is a rule to remember anyway, the less typing variant is preferable.

If V is just a personal project (developed by developer himself and used by himself), nothing is wrong to use whatever syntax the developer like

I am not Alex, but I very much like the shortcut syntax, and I am sure many others also like it too. If you do not like it, that is your right too, but to accuse Alex, that it was just his personal preference is wrong.

So the question is, why? Why it's ok here and there, but not in V UI module? Which part is wrong? Where's the god damn "one-way" philosophy?

As Alex answered you already in https://github.com/vlang/v/commit/a66eebc651c05ac2b9d018c3e11d06dbec1a6c94 , the "one-way" philosophy is not an absolute, but a guiding principle that has to be balanced with others like readability and simplicity.

@iredmail

About your pg example, there is a helpful button in github's interface, called "Blame". If you click it, you will see https://github.com/vlang/v/blame/master/examples/database/pg/customer.v#L13 that this was written 6 months ago, when there was no shortcut syntax implemented.

Also in that example, it is the only place that it would have benefited by the new syntax, so the need for the short syntax was not so pressing, as it was when coding several GUIs during the development of V's UI module.

Now that the new shortcut syntax is available, of course it can be applied to other places in V's main repository - PRs are welcome.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

choleraehyq picture choleraehyq  路  3Comments

oleg-kachan picture oleg-kachan  路  3Comments

markgraydev picture markgraydev  路  3Comments

vtereshkov picture vtereshkov  路  3Comments

elimisteve picture elimisteve  路  3Comments