3 ms·
While I agree with your ideas about the "syntax weight", I think that you're wrong about Java. Java definitely has no syntax for closures, because it has no se
by CodeMage 14y ago
While I agree with your ideas about the "syntax weight", I think that you're wrong about Java.
Java definitely has no syntax for closures, because it has no semantics for function types. It has interfaces and anonymous classes, which you can use to accomplish the same requirement, but the semantics of interfaces are not 100% the same as that of function types. A small example: there's only one function type for a function that takes one int and returns one int, whereas there are infinitely many interfaces you can define to represent that function type and none of them are necessarily interchangeable among themselves.
I think this distinction is important and that this is what the author is talking about when he says "there had better be a way to write a closure somehow!" In Java, there isn't.
- barrkel 14y agoYou're confusing structural / nominal typing with first class functions. What Java lacks is the latter, while closures do not depend on the former, which is what you're talking about. For example, C# supports function types, but it can have hundreds of function types (it calls them delegate types) for a function that takes one int and returns one int: delegate int Func1(int x); delegate int Func2(int x); delegate int Func3(int x); and so on; all these function types are incompatible with one another. I believe the distinctive feature of closures is closing over (hence the name) the outer lexical environment and binding to variables in the outer environment, in such a way that those variables are available during evaluation. Which is exactly what an anonymous class lets you have (so long as locals in the outer scope are declared final).