Skip to content

Instantly share code, notes, and snippets.

@kl0tl
Last active August 29, 2015 14:02
Show Gist options
  • Save kl0tl/12d46f02303ff97484dd to your computer and use it in GitHub Desktop.
Save kl0tl/12d46f02303ff97484dd to your computer and use it in GitHub Desktop.
Shorthand get/set accessors notation
/*
```
var foo = {};
get foo.computed() {
...
}
set foo.computed() {
...
}
```
*/
macro function_params {
rule { ($param:ident (,) ...) }
}
macro concise_function_expression {
rule { $params:function_params { $body ... } } => {
function $params { $body ... }
}
rule { $params:function_params => $body:expr } => {
function $params { return $body }
}
}
// cf http://stackoverflow.com/a/22414263
macro syntax2string {
rule { $syntax } => {
[makeValue(unwrapSyntax($syntax).toString(), $syntax)]
}
}
macro stringify {
case { _ ($syntax) } => {
letstx $string = syntax2string(#{$syntax});
return #{$string};
}
}
let get = macro {
rule { $name:ident $params:function_params => $body:expr } => {
get $name $params { return $body }
}
rule { $symbol:ident (.) ... $name:ident $function:concise_function_expression } => {
$symbol (.) ... . __defineGetter__(stringify($name), $function)
}
rule { $rest ... } => { get $rest ... }
}
let set = macro {
rule { $name:ident $params:function_params => $body:expr } => {
set $name $params { return $body }
}
rule { $symbol:ident (.) ... $name:ident $function:concise_function_expression } => {
$symbol (.) ... . __defineSetter__(stringify($name), $function)
}
rule { $rest ... } => { set $rest ... }
}
export get;
export set;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment