public
Created

Exploring Dart via the ERights money example

  • Download Gist
money.dart
Dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
 
// compare with https://bitbucket.org/DanC/coffee-craft/src/682d06f02e99/money.coffee
 
// ugh... no nested classes? gotta pass thunks around?
class SealedBox {
final capture;
SealedBox(this.capture);
shareContent() { capture(); }
}
 
class Sealer {
final save;
Sealer(this.save);
seal(it) => new SealedBox(() {save(it);});
}
 
class Unsealer {
final flush;
final bad;
final fill;
final extract;
Unsealer(this.flush, this.bad, this.fill, this.extract);
unseal(box) {
flush();
box.shareContent();
if (bad())
throw new IllegalArgumentException('invalid box');
fill();
flush();
return extract();
}
}
 
makeBrandPair(hint) {
final noObject = new Object();
var shared = noObject;
var content = null;
final sealer = new Sealer((it) {shared = it;});
 
final unsealer = new Unsealer(
() { shared = noObject;},
() => shared == noObject,
() { content = shared; },
() => content );
 
return [sealer, unsealer];
}
 
main() {
// ugh... ;s everywhere
print('eyeball-test makeBrandPair');
// no destructuring assignment?
final s_u = makeBrandPair('bob');
final s = s_u[0];
final u = s_u[1];
final x = s.seal('abc');
print(x);
print(u.unseal(x));
}
 
/*
# connect the dots between node.js's console
# and http://wiki.erights.org/wiki/TextWriter
out = {
print: (s) -> console.log(s)
}
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.