carbon-language / carbon-lang

Carbon Language's main repository: documents, design, implementation, and related tools. (NOTE: Carbon Language is experimental; see README)
http://docs.carbon-lang.dev/
Other
32.26k stars 1.48k forks source link

Design idea: string interpolation #2005

Open jonmeow opened 2 years ago

jonmeow commented 2 years ago

Per discussion on #1811, there's interest in string interpolation but it's not an area that's been closely examined. This is an area where a proposal would be helpful.

https://en.wikipedia.org/wiki/String_interpolation gives some information and provides some cross-language examples which may be helpful for proposal background and thinking about options. It's important to consider the syntax that multiple languages are looking at.

In Rust, I think https://www.rustnote.com/blog/format_strings.html and https://github.com/rust-lang/rfcs/pull/2795 may provide some interesting ideas and alternatives to think about. Note though that while Print is using meta-programming in Rust, the leads have expressed interest in exploring non-meta-programming-based solutions first.

As a possible data source, it may be worth poking at a few sourcegraph searches (such as this one) to see what developers are doing today.

OlaFosheimGrostad commented 2 years ago

I would like to give this a go.

Niikelion commented 2 years ago

Javascript have pretty basic, but easy to understand and use string interpolation(template strings): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals . C# syntax, while being more complex(it incorporates option to specify data formats) is also worth considering in my opinion: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated . If you want to discuss syntax and/or possible ways to approach implementation I will be more than happy to help.

OlaFosheimGrostad commented 2 years ago

C# syntax, while being more complex(it incorporates option to specify data formats) is also worth considering in my opinion: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated . If you want to discuss syntax and/or possible ways to approach implementation I will be more than happy to help.

Yes, I am thinking that maybe $"…" is a good solution. I am trying to map out various use scenarios and various string-buffer-targets in order to get good performance. High level languages create many temporary strings, and it would be nice if we could avoid that for some use cases.

I don't know which discord channel we can use for interpolated strings yet, but feel free to share ideas in this issue, I read it regularly.

OlaFosheimGrostad commented 2 years ago

We might end up needing to support C++20 std::formatter, so it is possible that our hands are tied as far as number formatting goes. On the upside, the C++20 formatting spec is pretty non-controversial, a lot like Python for instance.

Niikelion commented 2 years ago

I don't think that we are constrained by std::formatter syntax in designing string interpolation, but I agree that it is a good idea to get some inspiration from it. Format in interpolation could be specified like this: $"{pi: 10f}" and would roughly translate to std::format("{:10f}", pi);. I think that we can skip all cases that require passing additional arguments to format, like std::format(":{}.{}f",pi, 10, 5); and hope that value : value is not a valid value expression in Carbon.

OlaFosheimGrostad commented 2 years ago

I don't think that we are constrained by std::formatter syntax in designing string interpolation

We might be if we want to support formatting of C++ types, see chrono for instance: https://en.cppreference.com/w/cpp/chrono/local_t/formatter

(Constrained in the sense that we can do a rewrite to what C++ formatter expects.)

Tomttth commented 2 years ago

I was going through wikipedia and found this groovy feature interesting:

final n = 52
def sentence = "n is ${n%2 == 0 ? "even" : "odd"}"
println sentence

It would be good if we can compute the value in the string literal itself.

we can fine tune in this way for carbon:

$"n%2==0?'even':'odd'"
OlaFosheimGrostad commented 2 years ago
$"n%2==0?'even':'odd'"

Carbon appears to be using if cond then value else value for conditional expressions, so one option might be to write this as:

let sentence:auto = $"n is { if n%2 == 0 then "even" else "odd" }";
OlaFosheimGrostad commented 2 years ago

We now have a channel on Discord, feel free to share your thoughts here:

https://discord.com/channels/655572317891461132/1009125434929201172

geoffromer commented 2 years ago

That Groovy example talks about developers in sexist and ageist terms, and we don't do that here. I know it's copied verbatim from Wikipedia, but that kind of language can still be hurtful when it's inside quotation marks. I've replaced it with a more innocuous example in the comments above (and also fixed the formatting, because the $s were confusing GitHub).

holmbuar commented 2 years ago

This is what I see when clicking the link to Discord from this thread. Am I doing it wrong?

image

jonmeow commented 2 years ago

@torlarse The link works fine for me. It's #string-interpolation on Discord. Note, if there are any Discord-specific followups, please use a separate discussion to let this issue stay focused.

Tomttth commented 2 years ago

I alao faced the same issue, when I was trying to access the link

On Sun, 21 Aug, 2022, 9:24 pm Jon Ross-Perkins, @.***> wrote:

@torlarse https://github.com/torlarse The link works fine for me. It's

string-interpolation on Discord

https://github.com/carbon-language/carbon-lang/blob/trunk/CONTRIBUTING.md#collaboration-systems. Note, if there are any Discord-specific followups, please use a separate discussion https://github.com/carbon-language/carbon-lang/discussions to let this issue stay focused.

— Reply to this email directly, view it on GitHub https://github.com/carbon-language/carbon-lang/issues/2005#issuecomment-1221572291, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWCZDJ3D575RWGAVZ2HLOJTV2JGNBANCNFSM56J6BLXQ . You are receiving this because you commented.Message ID: @.***>

github-actions[bot] commented 1 year ago

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please comment or remove the inactive label. The long term label can also be added for issues which are expected to take time. This issue is labeled inactive because the last activity was over 90 days ago.