Skip to content

Instantly share code, notes, and snippets.

@mrspeaker
Last active August 29, 2015 14:18
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 mrspeaker/ce1cc886c8324e9b8bd2 to your computer and use it in GitHub Desktop.
Save mrspeaker/ce1cc886c8324e9b8bd2 to your computer and use it in GitHub Desktop.
JS immutable identifier binding
/*
A sweet.js macro for adding a JS identifier
binding (`val`) that is the same as a `const` declaration,
that is then frozen with `Object.freeze`.
*/
'use strict';
macro val {
rule {
$Identifier:ident = $Initializer:expr;
} => {
const $Identifier = Object.freeze($Initializer);
}
}
val a = [1,2,3,4];
val b = { test: "yep" };
a.push(5);
>> TypeError: a.push(...) is not extensible
b.test = "nope";
>> TypeError: "test" is read-only
@mrspeaker
Copy link
Author

This is a sweet.js macro. Test it out in the editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment