String interpolation #1086
Replies: 7 comments 12 replies
-
Is there actual any practical benefit for needing the annotation? It doesn't look like these interpolated strings can be passed around unfilled, eg you couldn't do: let template = "#{name}'s score is #int{score}"
template(name: "Andy", score: 9001) so what do we gain by requiring the annotation? |
Beta Was this translation helpful? Give feedback.
-
Question, Would the output of interpolation be a String or a String Builder? I could see performance benefits of String builder. |
Beta Was this translation helpful? Give feedback.
-
Perhaps having typeclasses as discussed further here would make the syntax for this better. The string interpolation would only be able to work when a displayable type is passed in, and then there would be no need for the explicit typing inside of the interpolation. |
Beta Was this translation helpful? Give feedback.
-
I generally prefer curley braces, for readability and also because you rarely actually type I suspect that escaping will break interpolation? so What about having another string quotation mark like javascript does with its backticks for interpoaltion, does that help with ease of implementation and/or speed? Edit 1 As for explicit types I would just make them appear the same as in function heads: ${score: Int}
// ...and/or...
${score:Int}
// ...the formatter would then decide what is recommended Edit 2 As for the |
Beta Was this translation helpful? Give feedback.
-
I just discovered: gleam-lang/suggestions#118 Maybe there should just be one way, not two, for interpolation and templating? // Single line
"Score: {{ score }}"
// Heredoc
"""
Score: {{ score }}
""" When we start with just |
Beta Was this translation helpful? Give feedback.
-
Hi, might consider sticking with the backslash escape, as it looks like gleam already uses it for replacement: https://tour.gleam.run/basics/strings/
|
Beta Was this translation helpful? Give feedback.
-
Personally, I would prefer the type to go outside the brackets/braces (like case user_input {
"$Int{number}" -> "You entered the integer: $Int{number}"
"$Float{number}" -> "You entered the float: $Float{number}"
_ -> "Please enter either an integer or a float"
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently to join together strings in Gleam we have to use a library that calls out to the host language (i.e. Erlang, JavaScript). This is also true of other string operations such as converting ints and floats to strings.
It is rather verbose in practice.
With an interpolation syntax we could have these common operations be more concise and more portable across target languages.
This would be similar in function to the existing bit string literal syntax which permits the programmer to construct bit strings from multiple parts, with annotations to indicate the types of each part.
Edit: An issue has been created #1473
Interpolatable types
String
- The default. This can be annotated asstring
, but as the default the annotation is optional.Int
- Annotated asint
Float
- Annotated asfloat
anything
- Annotated asdebug
,any
, orraw
? Name tbd. Converts any type to a string. There's no guarentee about the output format, it is platform specific and may change between versions.Possible syntaxes
"#{name}'s score is #{score:int}!"
"#{name}'s score is #int{score}!"
"$name's score is $score:int!"
"${name}'s score is ${score:int}!"
"${name}'s score is $int{score}!"
Future expansions
This syntax could also possibly be used for pattern matching on string prefixes.
More annotations could be added to control the manner in which items are formatted.
Prior art
Beta Was this translation helpful? Give feedback.
All reactions