Fantom

Login | Register

Ensure closure parameters are named #1284

rosarinjroy
31 Oct 2010

Consider the following code. It doesn't do anything useful, except to explain my point using the Func inside another Func.

using concurrent

// Test class.
class Main {
   Void main() {
     actor := Actor(ActorPool()) |Obj? -> Str?| {
        Obj? inst := Actor.locals.getOrAdd("anInstance", |Str -> Obj?| { return "Hello" })
        return inst?.toStr
     }
   }
}

If I compile this, I am getting the following error:

C:\Users\Roy\fantom\fan\nestedfunc.fan(7,60): Closure parameter '_a' is already defined in current block
ERROR: cannot compile script

If I name the argument in the inner Func, then it compiles fine. The working code is given below:

using concurrent

// Test class.
class Main {
   Void main() {
     actor := Actor(ActorPool()) |Obj? -> Str?| {
        Obj? inst := Actor.locals.getOrAdd("anInstance", |Str key -> Obj?| { return "Hello" })
        return inst?.toStr
     }
   }
}

Here is what I think: The nested Func blocks should name their args so that they don't conflict. One possible scheme I can think of is "_a_1, _b_1, .." for the first level, and "_a_2, _b_2, .." for the next nested level, and so on.

I am also thinking that naming args as "_a" could conflict with a variable name user might have, as its possible (though not common) to have a variable name "_a".

brian
31 Oct 2010

Promoted to ticket #1284 and assigned to brian

Actually parameter names are supposed to be required for closures, so that is a bug. Parameter names can only be omitted in type signatures. Although it looks like the parser (and the grammar) don't handle this distinction. Thanks for reporting.

brian
8 Nov 2010

Renamed from May be a bug with naming of parameters in nested Funcs to Ensure closure parameters are named

brian
8 Nov 2010

Ticket resolved in 1.0.56

Ensure formal parameters to a closure are always given a name

Login or Register to Reply

Back | All Topics