Skip to content
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

Closed
wants to merge 5 commits into from
Closed

Conversation

ailisp
Copy link

@ailisp ailisp commented Feb 18, 2020

Add typescript like string interpolation: foo ${variable} bar by @willemneal

if (acc == null) return expr;
return Node.createBinaryExpression(Token.PLUS, acc, expr, tn.range(startPos, tn.pos));
}, null);
}
Copy link
Member

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].

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Member

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 i32s, String#toString (returning itself) for strings 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.

Copy link
Contributor

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>().

Copy link
Contributor

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.

Copy link
Contributor

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.

@MaxGraey
Copy link
Member

MaxGraey commented Aug 1, 2021

Already implemented in #1715 so closing this.

@MaxGraey MaxGraey closed this Aug 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants