Skip to content

Instantly share code, notes, and snippets.

@kl0tl
Last active August 29, 2015 14:02
Show Gist options
  • Save kl0tl/b66f2c70fee61c010ead to your computer and use it in GitHub Desktop.
Save kl0tl/b66f2c70fee61c010ead to your computer and use it in GitHub Desktop.
Parens free `switch` expression
/*
Multiple expressions per case are allowed
The result of the matching case, or the optional default, is automatically returned
```
var x = switch os {
case 'osx', 'ios': 'apple'
default: os
};
```
*/
let switch = macro {
rule { $value:expr { $body ... } } => {
(function () {
switch ($value) {
$body ...
}
})()
}
}
let case = macro {
rule { $($pattern:expr (,) ...): $return:expr } => {
$(case $pattern:) ...
return $return
}
}
let default = macro {
rule { : $return:expr } => {
default:
return $return
}
}
export switch;
export case;
export default;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment