For some reason, I had become convinced that the only way to make the Syntax
static from*
methods available to compiletime code was via modules. But in fact they can, and actually should, be put on Syntax
instances. This would let you do cool things like:
syntax to_str = ctx => {
let argStr = ctx.next().value.val();
let dummy = #`here`.get(0);
return #`${dummy.fromString(argStr)}`;
}
let s = to_str foo
// expands to:
// let s = 'foo'
The reason we actually want to make from*
instance methods is that most of the time when creating a new syntax object you need to re-use an existing syntax object's lexical context (the third argument to the static Syntax.from
method). Having these methods as instance methods means you get the lexical context stealing behavior "for free".