_This issue was originally filed by grob...@gmail.com_
In JavaScript Partial Application and Currying is wellknown. Things like this are possible:
{{{code}}}
numberGame() {
var secretNumber = 21;
return () {
guess(g) {
if(g == secretNumber) {
return "Correct";
} else {
return "Try again";
}
}
};
}
main() {
var game = numberGame();
print( game.guess(45) );
}
{{{code}}}
This is not possible with Dart, because in this case JavaScript would return an Object with a new function.
As an alternative approach, Dart might be benefit from implementing anonymous classes. Like:
{{{code}}}
interface A {
void doSomething();
}
myFunction() {
return new A() {
void doSomething() {
// do something
}
}
}
numberGame() {
return new Dynamic() {
int guess(g) {
}
}
}
{{{/code}}}
This is a known appoach even in the Java World.
Please see:
https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/891044daf1754150
_Set owner to @gbracha._
_Added Area-Language, Triaged labels._
We may consider object literals in due course. Renaming the issue since it has nothing to do with the title.
_Removed Type-Defect label._
_Added Type-Enhancement, Accepted labels._
_Changed the title to: "Support object literals or anonymous classes"._
Issue #3028 has been merged into this issue.
You can emulate this easily enough with a class being constructed with a closure argument to capture scope etc. Benefit outweighed by complexity.
_Added WontFix label._
_This comment was originally written by @tomyeh_
Sorry, I don't get it. Would you show me the sample code equivalent to anonymous classes? For example, how to do the following code in Dart? Thanks.
return new Iterator<String>() {
bool hasNext() {...}
String next() {...}
};
_This comment was originally written by potoms.j...@gmail.com_
Tom,
I already started a thread about this issue. https://groups.google.com/a/dartlang.org/forum/#!topic/misc/rUWJMuDfJWc
Bummer this didn't happen.
Most helpful comment
Bummer this didn't happen.