-
-
Notifications
You must be signed in to change notification settings - Fork 647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
String interpolation #1115
String interpolation #1115
Conversation
if (acc == null) return expr; | ||
return Node.createBinaryExpression(Token.PLUS, acc, expr, tn.range(startPos, tn.pos)); | ||
}, null); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally this would generate a TemplateLiteral node holding the nodes of the template literal plus having a step to compile such a node in the compiler, instead of transpiling. Otherwise the AST cannot be reused for syntax highlighting in a future IDE extension. On such a node, I could imagine properties like stringParts[N]
, expressionParts[N-1]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh good point. Should we also add a default toString
to all reference types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My vote is that the expressions must have a type String
in the expressionParts
field. Although, it would be nice to check for an empty toString()
method. I realize this might not be possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The basic types already have #toString
implementations, like I32#toString
for i32
s, String#toString
(returning itself) for string
s etc. Typically one would resolve the expression parts, obtaining their type, and error if there is no toString
or call it otherwise before concatenating with string parts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I vote for a default toString() method, that uses something like nameof<T>()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's entirely possible to do something like this:
if (!isDefined(obj.toString())) ERROR("Must implement toString()");
As-pect uses this technique now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the compiler context, perhaps this might not be so useful, actually.
Already implemented in #1715 so closing this. |
Add typescript like string interpolation:
foo ${variable} bar
by @willemneal