Repository navigation
all: support short lambda expressions like a.sorted(|x,y| x > y), in all callsites that accept a fn callback - #19390
Conversation
4cb0e4a to
cbe975b
Compare
|
Hmm... |
import os
fn f( cb fn() string ) string { return cb() }
dump( f(fn() string { return os.args[0] }) )
// the same, just shorter:
dump( f(||os.args[0]) ) prints: i.e. using functions/expressions for callbacks, that get their values from external sources |
|
Must be too subtle for me... I don't get why the |
Because otherwise it would not be a lambda expression/function to call; it would get evaluated immediately. It may be easier to see with: import rand
fn f( cb fn() int ) string { return 'randoms: ${cb()}, ${cb()}, ${cb()}' }
dump( f(||rand.int()/1_000_000) )which will produce something like: i.e. the passed lambda expression will be executed 3 times, each with a different result. Note, that the above is equivalent to this, just shorter: import rand
fn f( cb fn() int ) string { return 'randoms: ${cb()}, ${cb()}, ${cb()}' }
dump( f( fn() int { return rand.int()/1_000_000 } )) |
|
So the Still looks strange... |
I agree. It is just the logical reduction of the lambda syntax, for when the anon fn has 0 args. |
|
awesome feature. could we maybe use |
|
Probably the same idea... it would be identified as an anonymous function instead of a lambda, without the |
|
Looking at this I actually sort of like this tiny syntax sugar. Shouldn't we also emphasize in the documentation that it is actually not a true lambda expression as known from other languages (which distinguish between a function and a lambda) but just syntax sugar for plain old V function definition? |
…all callsites that accept a fn callback (vlang#19390)
a.sorted(|x,y| x > y), in all callsites that accept a fn callbackv build-tools