I want to design a method that accept zero param function, like this:
process(|->| f) { f() }
Now the user of my method can invoke it in like this:
process |->| { echo("test") }
But I really wish user can just do this, but it won't work:
process { echo("test") } // Failed to compile: Cannot apply it-block to Void expr
I was just about to settle that this is the way Fantom syntax works, but then I realized Fantom allow you to call a single param function without declaring that param! Fantom would provide "it" implicitly in that case. This means I can rewrite my method like that, and just not use the param:
process(|Obj? notused| f) { f(null) }
And now my user is able to call it the way I want, which nice and short form:
process { echo("test") } // Yeah!
So the question is, can we improve the zero parameter function invocation? The hacked solution works, but it's not elegant, and definately not good for API documentation purpose.
Thanks, ~ Zemian
brianThu 15 Dec 2011
This is a deliberate design decision. It-blocks are a special case of closures, but they are not allowed to use the return keyword. Anything else has the |...| prefix to clearly indicate that we are creating a closure. So it was sort of a grand compromise made a couple years ago to use closures for declarative code in it-blocks, but still keep code readable when using things like return inside a code block.
I am not sure that is a restriction that has to last forever though.
saltnlight5Thu 15 Dec 2011
Okay. I understand the It-Block design reason. It's just from a developer perspective, the syntax usage is not consistent. Let me give you another example with methods in "sys" pod.
10.times { echo("Do things without an index counter.") }
10.times |Int i| { echo("Do things with counter: $i") }
This is great. Without understanding It-Block behind it tough, a developer would just conclude that Fantom is smart enough to "optionally" allow me to use a parameter or not. But then if they come to Env.cur.addShutdownHook, it will throw them off:
Env.cur.addShutdownHook { echo("I am exiting.") } // Oh oh, failed?
Env.cur.addShutdownHook |->| { echo("With some digging, now I will exit." }
So It-Block or not, I just want to give you some feedback as new user trying out the language and its syntax. In this regard I think it's inconsistent.
brianSat 17 Dec 2011
yeah it is not perfect, that is the case of two rules interacting in a weird way
saltnlight5 Thu 15 Dec 2011
Hi,
I want to design a method that accept zero param function, like this:
process(|->| f) { f() }Now the user of my method can invoke it in like this:
process |->| { echo("test") }But I really wish user can just do this, but it won't work:
process { echo("test") } // Failed to compile: Cannot apply it-block to Void exprI was just about to settle that this is the way Fantom syntax works, but then I realized Fantom allow you to call a single param function without declaring that param! Fantom would provide "it" implicitly in that case. This means I can rewrite my method like that, and just not use the param:
process(|Obj? notused| f) { f(null) }And now my user is able to call it the way I want, which nice and short form:
process { echo("test") } // Yeah!So the question is, can we improve the zero parameter function invocation? The hacked solution works, but it's not elegant, and definately not good for API documentation purpose.
Thanks, ~ Zemian
brian Thu 15 Dec 2011
This is a deliberate design decision. It-blocks are a special case of closures, but they are not allowed to use the
returnkeyword. Anything else has the|...|prefix to clearly indicate that we are creating a closure. So it was sort of a grand compromise made a couple years ago to use closures for declarative code in it-blocks, but still keep code readable when using things likereturninside a code block.I am not sure that is a restriction that has to last forever though.
saltnlight5 Thu 15 Dec 2011
Okay. I understand the It-Block design reason. It's just from a developer perspective, the syntax usage is not consistent. Let me give you another example with methods in "sys" pod.
We can use
Int.timesin the most natural ways.10.times { echo("Do things without an index counter.") } 10.times |Int i| { echo("Do things with counter: $i") }This is great. Without understanding It-Block behind it tough, a developer would just conclude that Fantom is smart enough to "optionally" allow me to use a parameter or not. But then if they come to Env.cur.addShutdownHook, it will throw them off:
Env.cur.addShutdownHook { echo("I am exiting.") } // Oh oh, failed? Env.cur.addShutdownHook |->| { echo("With some digging, now I will exit." }So It-Block or not, I just want to give you some feedback as new user trying out the language and its syntax. In this regard I think it's inconsistent.
brian Sat 17 Dec 2011
yeah it is not perfect, that is the case of two rules interacting in a weird way