Created
November 13, 2012 10:22
-
-
Save jedahu/4065056 to your computer and use it in GitHub Desktop.
sweet.js simple enum macro
This file contains hidden or 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
macro $enum { | |
case $name:ident {$val:ident (,) ...} => { | |
var $name = {$($val: null) (,) ...}; | |
for (var k in $name) { | |
$name[k] = k; | |
} | |
Object.freeze($name); | |
} | |
} | |
// Once sweet.js has syntax-case the for-loop | |
// will be unnecessary. | |
$enum Suites { CLUBS, SPADES, HEARTS, DIAMONDS } | |
// Equivalent to: | |
// | |
// var Suites = Object.freeze( | |
// { CLUBS: 'CLUBS' | |
// , SPADES: 'SPADES' | |
// , HEARTS: 'HEARTS' | |
// , DIAMONDS: 'DIAMONDS' | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A "better"
ident
tostring
implementation could be the following: