ceylon / ceylon-spec

DEPRECATED
Apache License 2.0
108 stars 34 forks source link

make void optional in anonymous functions #1396

Closed gavinking closed 9 years ago

gavinking commented 9 years ago

Currently we let you drop the function keyword for anonymous functions that return a value, but we don't let you drop void for anonymous functions which don't return anything. It seems to me that this is too heavy-handed, and isn't very friendly for use with methods like Iterable.each(). I don't see why we couldn't relax this, letting you write:

stream.each((x) {
    print(x);
});
FroMage commented 9 years ago

+1

quintesse commented 9 years ago

Indeed +1

lukedegruchy commented 9 years ago

I can already do this in Ceylon:

value list = ["1","2","3"];
list.each(print);

I'm guessing this proposal applies to more complex anonymous void functions?

quintesse commented 9 years ago

@lukedegruchy pretty sure that's the case yes

gavinking commented 9 years ago

@lukedegruchy your example is a function reference. I'm taking about anonymous functions, particularly multi-line anon functions.

lukedegruchy commented 9 years ago

So, in other words, you want to do this:

list.each((x) { 
    y = x + x;
    print(y); 
});

But need to do this instead:

list.each(void (String x) { 
    value y = x + x;
    print(y); 
});
gavinking commented 9 years ago

This is done, but I need to document it in the spec.

gavinking commented 9 years ago

Done.

gavinking commented 9 years ago

OK, I've found one minor issue here that I'm not sure what to make of. This anonymous function:

() { throw; }

Used to be assigned the type Nothing, but now it's assigned the type Anything. I'm not sure that's a good thing,

gavinking commented 9 years ago

I think this is all good. Closing.