Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Created May 13, 2014 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronpowell/6f8b72be982bfdcc7293 to your computer and use it in GitHub Desktop.
Save aaronpowell/6f8b72be982bfdcc7293 to your computer and use it in GitHub Desktop.
Type annotations and checking with Sweet.js
macroclass typedef {
pattern { $x:ident : $type:ident }
pattern { $x:ident : $type:lit }
}
let function = macro {
rule { $name ($params:typedef ...) { $body ... } } => {
function $name ($params$x ...) {
$(if (typeof $params$x !== typeof $params$type) {
throw new Error('Found type ' + (typeof $params$x) + ' but expected ' + typeof $params$type);
}) ...
$body ...
}
}
}
function foo (a : 0) {
console.log(a);
}
foo(1);
foo('a');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment