C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Newlines in F-strings

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Wed, 25 Oct 2023 10:44:10 +0200
śr., 25 paź 2023 o 01:32 Hadriel Kaplan via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> Hi,
>
> While talking with Bengt offline about various use-cases for f-strings, he brought up one that I'm not sure about.
>
> Should this compile? (note the 'R' for raw-string):
>
> FR"(name={
> get_name()
> }, time={
> std::system_clock::now()
> :%H:%M:%S
> })";
>
> Right now, in the next rev of the draft I'm working on, the above would treated identically as this non-raw f-string:
>
> F"name={\nget_name()\n}, time={\nstd::system_clock::now():%H:%M:%S\n}";
>
>
> And it would interpolate to this:
>
> std::format("name={}, time={:%H:%M:%S\n}",\n get_name()\n,\n std::system_clock::now());
>
> Note that there are multiple sets of the two characters "\" and "n" within the extracted arguments (not escaped newlines).
>

I think extracted arguments should be literal new lines characters not
`\` and `n`.
Logic should be that content of `R` is what `F` see and extract from
string literal.
let use character `"` as example:
```
F"{a(\"\")}" //-> "", a("")
RF"({a("")})" //-> "", a("")
```


> So it will fail to compile due to those characters.
>
> ---
>
> If you think it should compile, what should the format-string be after interpolation?:
>
> 1. "name={}, time={:%H:%M:%S\n}"
> or
> 2. "name={}, time={:%H:%M:%S}"
>
> And if (2), how do we know the programmer didn't want the newline? It is perfectly legal to have one there for chrono formatting, I believe.
>

how formater handle any other white spaces? overall tailing white
spaces could be outside of the format specifier if the user
would like to have them. When look how C# handle this, it reject last
white space but accept any other:
```
$@"a{1:

     x}b"
//evaluate to
"a\r\n \r\n xb"
```



> ---
>
> Personally I think it's ok for this scenario to fail to compile, but I also think it's ok to succeed instead.
>
> The programmer can always do this instead anyway, if they want to split things up:
>
> F"name={ get_name() }, "
> F"time={ std::system_clock::now():%H:%M:%S} )";
>
> -hadriel
>
>
>
> Juniper Public
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-10-25 08:44:20