I think the following code, or something like it ought to compile. The fourth echo statement is currently a compile error.
Notice the second echo statement is close, though wordy, but only works for static methods, because the current object reference is lost when you go though reflection.
The add method exactly matches the argument signature for takeFunc, so it sucks that you can't pass it in as you'd expect to be able to.
class Main
{
static Void main() { Main.make.run }
Int myVal := 5
Void run() {
echo( "4 = " + staticAdd(2, 2) )
echo( "4 = " + takeFunc(typeof.method("staticAdd").func) )
echo( "9 = " + add(2, 2) )
echo( "9 = " + takeFunc(add) )
}
static Int staticAdd(Int a, Int b) {
return a + b
}
Int add(Int a, Int b) {
return a + b + myVal
}
Int takeFunc(|Int, Int -> Int| f) {
return f(2, 2)
}
}
brianSat 28 Jan 2012
The add method exactly matches the argument signature for takeFunc, so it sucks that you can't pass it in as you'd expect to be able to.
In Fantom we allow you to omit the () parens, which means you can't pass around a method/function strictly by name like you would in a more functional language. Although I definitely like that technique, it isn't quite right for Fantom.
What you can do always do the long hand version:
takeFunc |a,b| { add(a,b) }
I actually will often do that just to keep things readable (for example I rarely use an it-block outside of a constructor).
If add was static you can also do this:
takeFunc(#add.func)
A long time ago we also had this syntax:
takeFunc(&add)
That is what you really want, but it didn't jive some other features so I took it out. I do want to go back and do a bit more in this space. Sort of been thinking of it similar to how Scala uses underbars. I actually use that syntax in our functional scripting language and I've come to really like it.
PatrickArnesen Fri 27 Jan 2012
I think the following code, or something like it ought to compile. The fourth echo statement is currently a compile error.
Notice the second echo statement is close, though wordy, but only works for static methods, because the current object reference is lost when you go though reflection.
The
add
method exactly matches the argument signature fortakeFunc
, so it sucks that you can't pass it in as you'd expect to be able to.brian Sat 28 Jan 2012
In Fantom we allow you to omit the
()
parens, which means you can't pass around a method/function strictly by name like you would in a more functional language. Although I definitely like that technique, it isn't quite right for Fantom.What you can do always do the long hand version:
I actually will often do that just to keep things readable (for example I rarely use an it-block outside of a constructor).
If add was static you can also do this:
A long time ago we also had this syntax:
That is what you really want, but it didn't jive some other features so I took it out. I do want to go back and do a bit more in this space. Sort of been thinking of it similar to how Scala uses underbars. I actually use that syntax in our functional scripting language and I've come to really like it.