Exploring Dart via the ERights money example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment