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 #1473

Closed
lpil opened this issue Jan 20, 2022 · 5 comments
Closed

String interpolation #1473

lpil opened this issue Jan 20, 2022 · 5 comments
Labels
discussion The approach has not yet been decided help wanted Contributions encouraged

Comments

@lpil
Copy link
Member

lpil commented Jan 20, 2022

Prior discussion: #1086

Syntax

let name = "Joe"
let messge = "Hello, ${name}!"

Here a variable is used with the ${} interpolation syntax, but any arbitrary Gleam expression is permitted.

Erlang output

Name = <<"Joe">>,
Message = <<"Hello, "/utf8, Name/binary, "!"/utf8>>.

JavaScript output

let name = "Joe"
let messge = `Hello, ${name}!`

Typing

The interpolated expression within the ${} must be of type String. In future we could permit other types, but not for this first version.

AST

I suggest that this AST node could be represented like so:

StringInterpolation {
  location: SrcSpan,
  segments: Vec<Expression>
}

When performing code generation we can check to see if each expression is a string literal or not and have a special case for that in order to get the right output syntax.

The implementor may determine that there is a better representation.

@lpil lpil added help wanted Contributions encouraged area:type-analysis labels Jan 20, 2022
@J3RN
Copy link
Member

J3RN commented Jan 20, 2022

Few implementation notes, feel free to correct me @lpil

  • Using double quotes " instead of backticks (a la JavaScript) `, the lexer will be a bit more exciting 😁
  • I assume these should all be valid and interpreted as verbatim text:
    • "Hello ${name"
    • "Hello $name}"
    • "Hello {name}"
  • Guessing these are errors:
    • "Hello ${}"
    • "Hello ${"Dr. ${name}"}"

@lpil
Copy link
Member Author

lpil commented Jan 20, 2022

  • "Hello ${name" : parse error, unexpected EOF
  • "Hello $name}" : string
  • "Hello {name}" : string
  • "Hello ${}" : parse error, unexpected {
  • "Hello ${"Dr. ${name}"}" : discouraged but valid nested interpolation

@J3RN
Copy link
Member

J3RN commented Jan 20, 2022

discouraged but valid nested interpolation

I'm assuming this is because the string interpolation is interpreted as a literal? Or do we want arbitrary expressions inside of interpolation? e.g.

"Hello, ${string.append("Wor", "ld")}"

@lpil
Copy link
Member Author

lpil commented Jan 20, 2022

Yes that's right, any arbitrary expression is to be permitted within ${}, similiar to Elixir, JavaScript or Ruby.

@lpil
Copy link
Member Author

lpil commented Nov 5, 2022

Closing this because I would like to see how far we get with <> for now.

@lpil lpil closed this as not planned Won't fix, can't repro, duplicate, stale Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion The approach has not yet been decided help wanted Contributions encouraged
Projects
None yet
Development

No branches or pull requests

2 participants