Sdk: Support object literals or anonymous classes

Created on 9 Jan 2012  路  7Comments  路  Source: dart-lang/sdk

_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

area-language closed-not-planned type-enhancement

Most helpful comment

Bummer this didn't happen.

All 7 comments

_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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matanlurey picture matanlurey  路  3Comments

xster picture xster  路  3Comments

sgrekhov picture sgrekhov  路  3Comments

DartBot picture DartBot  路  3Comments

brooth picture brooth  路  3Comments