Skip to content

Instantly share code, notes, and snippets.

@TooTallNate
Created July 16, 2014 05:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TooTallNate/baa25c0a58b7bd181bab to your computer and use it in GitHub Desktop.
Save TooTallNate/baa25c0a58b7bd181bab to your computer and use it in GitHub Desktop.
C-style assert() function for sweet.js, which include the assertion code in the Error object (as opposed to the calculated result). Similar to: https://github.com/visionmedia/better-assert
macro assert {
rule {($test ...)} => {
if (!($test ...)) {
throw new Error('AssertionError: ' + #{ $test ... });
}
}
}
var foo = 100;
assert(100 == foo);
// passes
assert(99 == foo);
// fails!
// Error: AssertionError: 99 == foo
// at blah.js:1:1
// at ...
@Havvy
Copy link

Havvy commented Oct 25, 2014

Does not work with SweetJS v.0.7.1.

You'd want this to be a case macro and have some code to string macro.

@CMCDragonkai
Copy link

One important use of assertions in C, is that the assertions go away once compiled. Can this be done using sweet?

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