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
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
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
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
numberGame() {
var secretNumber = 21;
return {
guess(g) {
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
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
Thanks Bob.
I will do that (tomorrow probably). After thinking some time about it
I think anonymous types are pretty neat.
Cheers
> Thanks!
> - bob
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;
/*** @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};
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.