Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Created September 24, 2013 14:19
Show Gist options
  • Save natefaubion/6685482 to your computer and use it in GitHub Desktop.
Save natefaubion/6685482 to your computer and use it in GitHub Desktop.
macro $testadd {
case { $$mac $expr } => {
var ident = makeIdent('test', #{ $$mac });
return withSyntax ($ident = [ident]) {
return #{
return $expr + $ident;
}
}
}
}
macro $wrap {
case { $$mac $expr } => {
var ident = makeIdent('test', #{ $$mac });
return withSyntax ($ident = [ident]) {
return #{
(function($ident) {
$testadd 12
})($expr);
}
}
}
}
$wrap 42
@natefaubion
Copy link
Author

Result:

(function (test$242) {
    return 12 + test;
}(42));

@disnet
Copy link

disnet commented Sep 24, 2013

Do you need to use makeIdent in $wrap? If you just put test directly in as the parameter everything works out:

macro $wrap {
    case { _ $expr } => {
        return #{
            (function(test) {
                $testadd 12
            })($expr);
        }
    }
}

@natefaubion
Copy link
Author

What about using just rule? The following fails to resolve to the same identifier also (just like above):

macro $testadd {
  rule { $expr } => {
    return $expr + test;
  }
}

macro $wrap {
  rule { $expr } => {
    (function(test) {
      $testadd 12
    })($expr);
  }
}

$wrap 42 

Is it expected that it should resolve? It seems like it should, but that could be debatable.

@disnet
Copy link

disnet commented Sep 24, 2013

Yep, it makes perfect sense for rule to behave this way, it's just preserving hygiene. Just think of hygiene as preserving lexical scope. To find the binding of test inside $testadd all you should do is look at the scope at the definition of $testadd (not at the use of $testadd). Since there is no binding of test at the definition then we assume it's either unbound or bound as a global (and so don't perform any renaming).

If we were to bind the test introduced by expanding $testadd 12 to the binding in the IIFE this would be breaking hygiene.

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