v icon indicating copy to clipboard operation
v copied to clipboard

Allow only explicit auto-expand of []string in compile-time $for loops

Open dumblob opened this issue 4 years ago • 3 comments

See from https://github.com/vlang/v/pull/9208#issuecomment-796149607 to the end of the thread.

Main question is how should the expansion syntax look like? There is no exact equivalent in V, but there is ... for map-like expansions and other expansions.

Please write your syntax proposals below.

dumblob avatar Mar 12 '21 22:03 dumblob

Generally, x.$(my_string) turns a compile-time known string into an identifier (not an expression). But this feature turns an array of strings into a tuple of expressions. vweb uses it here: https://github.com/vlang/v/blob/master/vlib/vweb/vweb.v#L397

I don't understand the vweb code yet. Maybe if we had tuple variables that could work instead?

ntrel avatar Mar 13 '21 10:03 ntrel

Maybe if we had tuple variables that could work instead?

My train of thought: V doesn't have any tuples because so far there was no good reason to introduce them AFAIK -> V compile time (sub)language shouldn't have them either.

I actually think the compile time (sub)language shall be as close to V as possible syntactically. But I'm also curious how @mcastorina will use all this in vweb and what syntax alternatives V community will come up with.

dumblob avatar Mar 13 '21 13:03 dumblob

As @ntrel stated, vweb uses it to pass a variable number of arguments to user-defined methods. The number of arguments is specified by an attribute.

['/gh/:repo/issues/:issue']
fn (mut app App) issue(repo string, issue int) vweb.Result { /* ... */ }

Note that issue is an int and is auto-converted from a string.

So from vweb it would be used like..

$for method in T.methods {
    attrs := parse_attrs(method.attrs) // ['gh' ':repo' 'issues' ':issue']
    // url = '/gh/vlang/issues/42'
    method_args := create_url_params(url, attrs) // ['vlang' '42']
    app.$method(method_args)
}

mcastorina avatar Mar 13 '21 13:03 mcastorina