-
Notifications
You must be signed in to change notification settings - Fork 260
[SUGGESTION] Expose string interpolation outside string literals #271
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
Comments
I kind of like this approach. It feels minimalistic (which is a good thing IMHO) but can be a bit hard to grasp at first especially when the current approach is familiar to cpp programmers. But I still think that it could be integrated into cpp2 with some minor tweaks. |
Thanks. Now I've written the output of examples. |
I've changed the suggested feature to use |
|
Thanks, you're right, and I intentionally choose
If it feels |
I should think more about it to be simpler and more familiar to programmers 😰 😁. Thanks everyone. |
DISCLAIMERS TO SET EXPECTATIONS
This suggestion is inspired from this pull request. The syntax of this suggestion "how to achieve string interpolation outside string literals" is not important, and it can be anything that fits into C++2.
Currently we have 4 string literals:
Does it resemble this video about C++1 initialization? Maybe a little.
C++2 can use the following literal instead of the above literals:
But how can we have other string literals? We don't need any other string literals to achieve those featues. C++2 can mix raw string literals with capture expressions. C++2 already considers to use capture expressions (
...$
) inside interpolated string literals ("..."
), C++2 can go further and join them instead of introducing a new string literal for it.Currently this is what we have in C++2:
Now, the above line can be written like this:
They look like similar, they do the same thing but in a different approach.
The first one uses interpolated string literal but the second one joins raw string literal
"I'm waiting for "
and capture expressionname$
and raw string literal" to come home."
.On the other hand, currently this is how we use escape sequences such as
\n
in C++2:So, how can we use escape sequences such as
\n
if we won't have non-raw string literals? This is how it can be written:They seem a little different, but they do the same thing in a different approach.
The first one uses non-raw string literal but the second one joins raw string literal
Message:
and escape sequencen'
and raw string literaluse uppercase letter.
. As you can see,\n
is not obvious in the first one butn'
is obvious in the second one.DESCRIBE DETAILS
I suggest to have only raw string literals (without prefix, just only
"..."
):And instead of having interpolated string literals, C++2 can automatically join string literals
"..."
and capture expressions...$
. I don't know what name is better to call the sequence of"..."
and...$
, but for now I pickup the name combination group.Also escape sequences which doesn't have parameters will be written in
...'
notaion instead of\...
notation, but escape sequences which have parameters will be written in capture expression notaion, for example: escape sequence\o{nnn}
will be written aso(nnn)$
.Capture expressions
...$
and escape sequences...'
must be outside string literals"..."
, otherwise they will be not evaluated. For example:While
"
is the only special character, it can be escaped with double""
inside string literals. In other words, two string literal will be joined together and a"
character will be inserted between them:The combination group should follow the prefix of the first string literal. In the following example,
left
,middle$
andright
have the same prefixu8
:Also it's possible to have a suffix:
But some rules should be followed:
n'
:n'
and other escape sequences can be used everywhere:...'
and...$
notations are postifix:In addition, string literals can be more integrated into the language for reflection and generation example:
If C++2 could allow directly using string literal for function name, then it can be changed to:
Alternatively, instead of rules 4, 5 and 6, we can have one rule: the first element of combination group should always be a string literal. In this way for reflection and generation we have to write this:
Will your feature suggestion add new keywords, new operators or ...?
Yes, it adds new syntax for escape sequences with
...'
notation. It is for using escape sequences only and...'
is not like an operator, it is like a character literal'...'
and a string literal"..."
as they are not operators either. Using...'
notaion for escape sequences, is compatible with the current language syntax.Also a feature has to be added to automatically join raw string literals and capture expressions and escape sequences together.
The
...'
notation is a right choice for escape sequences as it resembles character literals'...'
and of course it doesn't conflict with them. In addition, The...'
notation doesn't conflict with character literal prefix either, because C++2 compiler and programmers can easily distinguish them:NOTE 1: Briefly
'...'
is a character literal,x'
is an escape sequence andx'...'
is a character literal prefix, but there is a corner case ifx
was both defined as a escape sequence and as a character literal prefix:It's still a little ambiguous, therefore escape sequences shouldn't be the first element of combination group (see rule 6):
Will your feature suggestion increase code verbosity or readability?
It depends. Sometimes it may increase code verbosity.
Consider how the following line in current C++2:
is different from the following line:
More examples:
All
x
,y
,z
have the same value.More on capture expressions:
Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code?
No.
Will your feature suggestion automate or eliminate X% of current C++ guidance literature?
Yes. It will do in the following ways:
Interpolated string literals will be integrated into the language outside of literals, this will open new powerfull features to be used instead of introducing new escape sequences for string literals, for example:
There is 2 way to describe how to read a combination group of
"..."
and...$
in this example:The first way is: Think about
"
s as an on/off switch! Now, Let's read the above example in this way:"
will start a string literal:"My book is named
"
will start a capture expression:"book$
"
will again start a string literal:" and I bought it in
"
will again start a capture expression:"year$
"
will again start a string literal:" when I was young
"
will end everything:";
The second way is: Think about
"
s as nested expressions! Now, Let's read the above example in this way:"
opens the combination group."book$"
is the first nested expression."year$"
is the second nested expression."
closes the combination group.How the compiler can determine if
"
or...$
or...'
are the last part of the combination group? If the counted"
is even, and there is a space or an operator or;
after them, then it has to be the end of the combination group.Describe alternatives you've considered.
Originally I considered to use escape sequences
\...
directly besides string literals"..."
and capture expressions...$
, but I gave up on this idea for the following example:Because it wouldn't work for
\nsecond$
, I had to place an extra character like'
to separate them:This approach would make it compelicated by having both
\...
and a separator'
when needed.After that, I decided to change escape sequence
\...
to be a postifix...\
:This could be a good solution, but it was better if C++2 could just thread all escape sequences as capture expressions:
By the way, using
...$
notation for escape sequences could be enhanced (because\n
,\r
, ... have to be a variable name liken$
,r$
, ... and it could prevent programmers to have local variables with those names).At the end, using
'
for escape sequences seems more natural as it is different from capture expressions thus it doesn't conflict with user defined variables such asn
andr
andt
:Originally the syntax was
q$
to escape"
character (when...$
was the notation for escape sequences) but later it changed to double""
which is visually more natural:The text was updated successfully, but these errors were encountered: