I wanted to re-use a local variable name (as I was dynamically generating code) and, thinking I'd be clever, I enclosed it in a code block (as you'd do in Java). What I wrote was similar to:
class Whoops {
Str thing := ""
Void main() {
thing += "wot"
{ // scope the local variables
a := "-ever"
thing += a
}
echo(thing)
}
}
If you run this, all that is printed is wot!?? I was very confused as to why -ever was not being appended.
It turns out that my code block is actually a with it-block and I had written:
Void main() {
thing += "wotsit".with {
a := "-ever"
thing += a
}
}
(still not sure where -ever went though.)
So I've now used a func instead:
Void main() {
thing += "wot"
|->| { // scope the local variables
a := "-ever"
thing += a
}()
}
SlimerDude Tue 8 Jul 2014
I thought I'd share this...
I wanted to re-use a local variable name (as I was dynamically generating code) and, thinking I'd be clever, I enclosed it in a code block (as you'd do in Java). What I wrote was similar to:
class Whoops { Str thing := "" Void main() { thing += "wot" { // scope the local variables a := "-ever" thing += a } echo(thing) } }If you run this, all that is printed is
wot!?? I was very confused as to why-everwas not being appended.It turns out that my code block is actually a
with it-blockand I had written:Void main() { thing += "wotsit".with { a := "-ever" thing += a } }(still not sure where
-everwent though.)So I've now used a func instead:
Void main() { thing += "wot" |->| { // scope the local variables a := "-ever" thing += a }() }Which prints
wot-everas expected.