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 iterpolation #70

Closed
ogoffart opened this issue Oct 2, 2020 · 7 comments
Closed

String iterpolation #70

ogoffart opened this issue Oct 2, 2020 · 7 comments
Labels
enhancement New feature or request rfc Request for comments: proposals for changes
Milestone

Comments

@ogoffart
Copy link
Member

ogoffart commented Oct 2, 2020

We want to be able to easily format strings with other expression.
Possible syntax:

  1. Same as javascript
 text:  `The window with is ${ root.width / 1px } and its title is ${ root.title }` ;

Advantage is that people coming from JS will be familiar with this.

  1. Using normal quote and use \{
text: "The window with is \{ root.width / 1px } and its title is \{ root.title }";

The advantage is that there is only one form of string. (I think JS uses backtick because using normal quote would not have been backward compatible, but we don't have this problem)


The question is how good does it play with translation?

Imagine we support this syntax:

text: tr!"The window with is \{ root.width / 1px } and its title is \{ root.title }";

The translation extracting tool could extract the string "The window with is {0} and its title is {1}" as the string to be translated.

@ogoffart ogoffart added enhancement New feature or request rfc Request for comments: proposals for changes labels Oct 2, 2020
@ogoffart
Copy link
Member Author

ogoffart commented Oct 2, 2020

From an implementation point of view, one would need to adjust the lexer to split the string in token that can be parsed independently: StringLiteral OpenInterpolation Identifier Dot Identifier ... CloseInterpolation StringLiteral OpenInterpolation ...
Then the parser could build an expression tree that would have a Expression::InterpolatedString { components: Vec<Expression> } that would then be produced.

@patrickelectric
Copy link
Contributor

Thanks for the implementation view, will take a closer look on it soon.

@Plecra
Copy link

Plecra commented Oct 13, 2020

Using a \ for the interpolation syntax is confusing to me. I'd expect it to be used to disable a special syntax, rather than mark it. I'd prefer "{foo}" to be interpolated, like Python's f strings.

@tronical
Copy link
Member

Ah yes, Python's f strings are like JS template strings - I also like that syntax a lot. And using regular quotes is easier than the backticks in JS. The more I think about it, the more I dislike those and prefer something like f strings.

I guess we just have to pick one and go with it.

@ogoffart
Copy link
Member Author

I tried to make a survey of the different language:
Please let me know if i got something wrong or forgot a language.
I only include the languages that allow to put any arbitrary expression within a string, so not just printf style

Javascript

Uses ` instead of " for the quote, then ${ ... } for the expression

`string text ${expression} string text`

Python

Require a f before the string, then expression are just in { }

f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'

C# / VisualBasic

String starts with $ and then inside is just { }

$"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now."

PHP

One must use " (and not ') then one can use $variable or ${variable}
One can put some expression within the ${ ... } but not all expression are supported.

"This works {$obj->values[3]->name}";

Swift

Normal string using \( ... )

"\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"

Rust

Rust does not have string interpolation, but the syntax of the format! macro will allow to use {variable} as per RFC 2795


Note that in the .60 language, there is not even yet way to escape quotes or anything. We don't have raw literal either.
But I guess that \ would be the most natural character to do escapes, since this is what almost all languages do (eg: \n and \") so it would be natural to use \{ }, then one does not need anything else special. This is almost what swift does.

@patrickelectric
Copy link
Contributor

I vote for python or javascript, maybe a mix between both languages since they are the cleaner and friendly approach for the user ?

// js
`string text ${expression} string text`
// python
f"string text {expression} string text"
// mix
`string text {expression} string text` or "string text {expression} string text"

I also agree with @Plecra comment about the escape char for interpolation.

@ogoffart ogoffart added this to the 0.0.5 milestone Jan 18, 2021
@ogoffart
Copy link
Member Author

Currently what's implemented in master is "string text \{expression} string text"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rfc Request for comments: proposals for changes
Projects
None yet
Development

No branches or pull requests

4 participants