Currying / Closures

62 views
Skip to first unread message

Christian Grobmeier

unread,
Dec 30, 2011, 6:25:04 PM12/30/11
to General Dart Discussion
Hi,

this code is not going to work:

numberGame() {
var secretNumber = 21;

return () {
guess(g) {
if(g == secretNumber) {
return "Correct";
} else {
return "Try again";
}
}
};
}

main() {
var game = numberGame();
print( game.guess(45) );
}

http://try.dartlang.org/s/Fk8l

This is something I can do with JavaScript. In Dart I would neet to
create an object of a known class outside numberGame() and return
that instead of the anonymous object in JavaScript. Is there a chance
I can solve this the "JavaScript" way?

Thanks + cheers!
Christian

--
http://www.grobmeier.de
https://www.timeandbill.de

Daniel Mendalka

unread,
Dec 30, 2011, 6:43:40 PM12/30/11
to Christian Grobmeier, General Dart Discussion
Your are using old well known JavaScript pattern to simulate private properties and functions. In Dart you don't need to do that – just add _ before secretNumber to make it private.

After just few changes this could looks like this: http://try.dartlang.org/s/qWIl

Rémi Forax

unread,
Dec 30, 2011, 6:53:43 PM12/30/11
to mi...@dartlang.org
On 12/31/2011 12:43 AM, Daniel Mendalka wrote:
Your are using old well known JavaScript pattern to simulate private properties and functions. In Dart you don't need to do that – just add _ before secretNumber to make it private.

After just few changes this could looks like this: http://try.dartlang.org/s/qWIl

Dart is more structured, you need to define classes and as a reward you get speed
and less memory consumption compared to JS.

Technically, if you want only speed, you don't need named class,
an anonymous frozen structure (no way to add fields after creation) like unamed class in C# is enough
but it will not give you a way to structure your program as named classes do.

Rémi

Quildreen Motta

unread,
Dec 30, 2011, 7:00:52 PM12/30/11
to mi...@dartlang.org
On 30/12/11 21:43, Daniel Mendalka wrote:
Your are using old well known JavaScript pattern to simulate private properties and functions. In Dart you don't need to do that – just add _ before secretNumber to make it private.

After just few changes this could looks like this: http://try.dartlang.org/s/qWIl

Well, what I got from the message was that Christian was claiming that
Dart handled closures incorrectly, but it turns out he was returning an
anonymous function, that defined a named function but didn't return the inner one.

Thus, surely the returned function would have no such thing as a `guess' method.

Also, the correct code here: http://try.dartlang.org/s/8yQl

Jay Young

unread,
Dec 31, 2011, 1:03:17 PM12/31/11
to mi...@dartlang.org, Christian Grobmeier
On Friday, December 30, 2011 6:43:40 PM UTC-5, Daniel Mendalka wrote:
Your are using old well known JavaScript pattern to simulate private properties and functions. In Dart you don't need to do that – just add _ before secretNumber to make it private.

After just few changes this could looks like this: http://try.dartlang.org/s/qWIl

Remember, "_"-prefixed names are only library-private, not class-private.  The secret number will still be visible to any code within that same library:  http://try.dartlang.org/s/PSUl

Normally, other code in the same library should be trustworthy, but this is still something to keep in mind.  There are no class- or instance-private members in Dart.


Seldaiendil D. Flourite

unread,
Jan 1, 2012, 6:38:55 PM1/1/12
to Christian Grobmeier, General Dart Discussion
BTW, you are returning a clousure than contains inside it's scope a function gess(g).

Maybe you should remove the () after the return.

numberGame() {
   var secretNumber = 21;
   return {
       guess(g) {

I dont know if that was what you was talking about

2011/12/31 Christian Grobmeier <grob...@gmail.com>

Christian Grobmeier

unread,
Jan 2, 2012, 4:15:12 AM1/2/12
to Seldaiendil D. Flourite, General Dart Discussion
On Mon, Jan 2, 2012 at 12:38 AM, Seldaiendil D. Flourite
<seldai...@gmail.com> wrote:
> BTW, you are returning a clousure than contains inside it's scope a function
> gess(g).
>
> Maybe you should remove the () after the return.
>
>> numberGame() {
>>    var secretNumber = 21;
>>    return {
>>        guess(g) {
>
>
> I dont know if that was what you was talking about

Actually I was speaking more of that kind:

numberGame() {
var secretNumber = 21;

return new Object() {


guess(g) {
if(g == secretNumber) {
return "Correct";
} else {
return "Try again";
}
}
};
}

I meant creating some kind of an anonymous object (without class),
which of course is not really the dart way. But anonymous
implementation of an interface should be the dart way imho

Cheers
Christian

Bob Nystrom

unread,
Jan 3, 2012, 2:29:51 PM1/3/12
to Christian Grobmeier, Seldaiendil D. Flourite, General Dart Discussion
On Mon, Jan 2, 2012 at 1:15 AM, Christian Grobmeier <grob...@gmail.com> wrote:

I meant creating some kind of an anonymous object (without class),
which of course is not really the dart way. But anonymous
implementation of an interface should be the dart way imho

Right. Dart doesn't currently have anonymous types, and I don't think they've come up for discussion much (not that I've overheard, at least). If it's something you're interested in, feel free to file a feature request with some details on how you'd like them to work and what problems they'd help you solve.

Thanks!
- bob

Christian Grobmeier

unread,
Jan 3, 2012, 2:35:07 PM1/3/12
to Bob Nystrom, Seldaiendil D. Flourite, General Dart Discussion

Thanks Bob.

I will do that (tomorrow probably). After thinking some time about it
I think anonymous types are pretty neat.

Cheers

> Thanks!
> - bob

Jay Young

unread,
Jan 4, 2012, 4:26:29 PM1/4/12
to mi...@dartlang.org, Bob Nystrom, Seldaiendil D. Flourite
I've been using Google's Closure Lib and Compiler for a while and have truly come to love their style of record types and typedefs as an augmentation to actual classes and interfaces.  In Javascript, this is done in JSDoc comments used by the Compiler's type checker.  For example, the following defines a type "Person" with three public properties that have the given types:

/**
 * @typedef {{name: string, age: number, usingDart: boolean}}
 */
var Developer;

Creating a class for small record types like this seems like supreme overkill when, coming from JS, you can say:

var someGuy = {name: "Jay", age: 27, usingDart: true};

In Dart, creating a class is overkill, using a map loses type checking of properties.  Record types w/ typedefs in Dart would be AWESOME.

Jay Young

unread,
Jan 4, 2012, 4:27:28 PM1/4/12
to mi...@dartlang.org, Bob Nystrom, Seldaiendil D. Flourite
On Wednesday, January 4, 2012 4:26:29 PM UTC-5, Jay Young wrote:
I've been using Google's Closure Lib and Compiler for a while and have truly come to love their style of record types and typedefs as an augmentation to actual classes and interfaces.  In Javascript, this is done in JSDoc comments used by the Compiler's type checker.  For example, the following defines a type "Person" with three public properties that have the given types:

/**
 * @typedef {{name: string, age: number, usingDart: boolean}}
 */
var Developer;

Of course, I meant "defines a type "Developer" with...".  Oops. 

Ladislav Thon

unread,
Jan 4, 2012, 4:59:42 PM1/4/12
to mi...@dartlang.org
/**
 * @typedef {{name: string, age: number, usingDart: boolean}}
 */
var Developer;

Creating a class for small record types like this seems like supreme overkill when, coming from JS, you can say:

Well, declared class looks very simillar:

class Developer {
  String name; num age; bool usingDart;
  Developer(String this.name, num this.age, bool this.usingDart); 
}

The only thing that looks redundant here is the constructor declaration. Surely that isn't what you are talking about, is it?
 
var someGuy = {name: "Jay", age: 27, usingDart: true};

var someGuy = new Developer("Jay", 27, true);

Is that really an overkill?

That said, "anonymous types" are surely interesting and I can imagine them being very useful in certain situations. Just not in this particular one.

LT

William Hesse

unread,
Jan 4, 2012, 5:39:52 PM1/4/12
to mi...@dartlang.org, Bob Nystrom, Seldaiendil D. Flourite
There seem to be some good ways to get type-checked map types, without
much work, in Dart.
You could use the type checking of function arguments when creating a map:

Function Developer = (String name, int age, bool usingDart) =>
   {name:name, age:age, usingDart:usingDart};
developer = Developer("Bill", 44, true);
These declarations can even be local to a class or function.

It seems to me that classes can be pretty lightweight. Only the
constructor in this is redundant -
perhaps default constructors for structs like this could be added.
The only fault is that classes
can't be nested or local. It is also convenient, once a class has
been added, to add any additional methods that might be useful on it,
so there is no barrier to using the record type abstractly if and when
that turns out to be useful.

class Developer {
String name;
int age;
boolean usingDart;
Developer(this.name, this.age, this.usingDart);
}


These other possibilities don't seem like they would be useful:

class Developer {
static final int name = 0;
static final int age = 1;
static final int usingDart = 2;
// optional
bool isDeveloper(x) => x[name] is String && x[age] is Number &&
x[usingDart] is boolean;
}
var developer = ['Bill', 44, true];
Assert(Developer.isDeveloper(developer));
print(developer[Developer.age]);

or

var developer = {name:"Bill", age:44, usingDart: true}; // Assuming
we get literals as keys
bool isDeveloper(x) => x['name'] is String && x['age'] is Number &&
x['usingDart'] is boolean;
isDeveloper(developer);

or

Developer = (String name, int age, bool usingDart) => [name, age, usingDart];

--
William Hesse
Software Engineer
whe...@google.com

Google Denmark ApS
Frederiksborggade 20B, 1 sal
1360 København K
Denmark
CVR nr. 28 86 69 84

If you received this communication by mistake, please don't forward it
to anyone else (it may contain confidential or privileged
information), please erase all copies of it, including all
attachments, and please let the sender know it went to the wrong
person. Thanks.

Reply all
Reply to author
Forward
0 new messages