Git Product home page Git Product logo

Comments (7)

ogoffart avatar ogoffart commented on May 14, 2024

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.

from slint.

patrickelectric avatar patrickelectric commented on May 14, 2024

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

from slint.

Plecra avatar Plecra commented on May 14, 2024

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.

from slint.

tronical avatar tronical commented on May 14, 2024

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.

from slint.

ogoffart avatar ogoffart commented on May 14, 2024

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.

from slint.

patrickelectric avatar patrickelectric commented on May 14, 2024

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.

from slint.

ogoffart avatar ogoffart commented on May 14, 2024

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

from slint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.