0

I want to replace this. with @ sign like in coffee script. I've write the macro:

macro (@) {
    case { return $a } => { return this.$a }
}

function LogSmth(name) {
    this.name = name;
    console.log(@name);
}

But got

SyntaxError: [macro] Macro `@` could not be matched with `name...`
57:     console.log(@name);

How to fix this?

2 Answers 2

3

Allow me to expand on Mike C's answer. What happens if we try and do something just with @ (a common operation is to bind an object to this). One might write: X.bind(@, ...) but this would fail with the above macro. Another possibility is the ability to do this: @['some property with a weird name'], but this would also fail with the above macro.

Here's my version:

macro @ {
  rule { [$x:expr] } => { this[$x] }
  rule { $x:ident } => { this.$x }
  rule {} => { this }
}

This also exposes one useful property about applying rules to macros which is that order matters.

1
  • Thanks. I made this one an answer for the future researchers. Mar 3, 2016 at 13:29
2

Cases have to return a syntax array. So you could fix yours by doing the following:

macro @ {
    case { _ } => { return #{ this. } }
}

Or you could produce this using a simple rule that doesn't use any patterns.

macro @ {
    rule {
    } => {
        this.
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.