-
Notifications
You must be signed in to change notification settings - Fork 5
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 and character literals V2 #71
Comments
This issue doesn't mention alignment and formatting. Using |
Indeed, because we haven't proposed anything for it. Alignment and formatting is a largely inelegant part of .NET (at least as in C#) IMO and I find it a tough problem to tackle.
This assumes the presence of a preprocessor which we might simply not need 😄 . But fair, we could think of some other character that's easier to type. |
When defining multiline string literals, I recommend specifying a fixed and predictable newline handling. I have experienced a bug before where the behavior was different on different machines, and it was tracked down to newlines in multiline string literals. You can easily configure git to change line endings locally. (Indeed, it is sometimes recommended for cross-platform development). As a result, the newlines in the source code were different on different developer machines and the build server. In C# whatever newlines are in the source code is what goes into the string. Instead, I think multiline string literals should always generate |
On the contrary, I highly advise you do not do this. If you must alter the code the user has written in some fashion inside a literal, then make it obvious; put a marker or something in the code to make it configurable. Concerns of file contents that change per-system, like line endings, should be left to the tools that manage the source code, like git itself (for example, by using a
That last group is the only one of the 3 that can take an action to have consistent behavior everywhere without falling out of constant value territory; the first two groups have to do some kind of transformation (either from CRLF to LF, or vice versa). Source code is the ultimate source of truth here, and doing anything else is just going to result in pain. |
Introduction
This issue aims to completely redesign the string literals, inspired by Swift string literals. The reason is that it essentially does everything the new C# literals do, but it's a more cleaned-up and less complicated version of them. I'd like to take this opportunity to slightly change character literals a bit to free up the single-quote character.
Escape sequences
The escape sequences would stay and be identical to what's already been specified.
Single-line string literals
Single-line string literals would start and end with double quotes and they can not span multiple lines. Example:
They can also contain the usual escape sequences:
In this latter example, the value of
x
would beMulti-line string literals
Multi-line string literals would start and end with 3 double-quotes. The string would start in the next line after the opening quotes and end before the line of the closing quotes. Example:
Note, that this string has no newlines in it. It is equivalent to the string
"Hello, World!"
. If you want a leading or trailing newline, you can do:The placement of the ending quotes determine the amount of whitespaces cut off from each line. Example:
Here, nothing is cut off, the string is exactly
But if we indent the ending quotes, we can cut off the leading whitespace:
Now the string is
Breaking long lines
Breaking long lines in multiline strings can be done using a
\
at the very end of lines. Example:Which equals to
"Hello, World!"
. This looks similar to C-style line continuations, but this is only valid in multiline-string literals.Note, that
#
changes the sequence here too (later section specifies what these are):Is literally
To have
Hello, World!
, you'd writeInterpolation
Interpolation introduces a new escape sequence, namely
\(
, which starts the interpolation expression until the matching)
. For example:Which would result in the string
1 + 2 = 3
.Alternatively, we could use
\{ ... }
or any other pairwise character.Extended string delimeter
The escape-sequences and starting and ending sequences of string literals of both single- and multi-line strings can be changed, to make pasting literal strings easier. This is done by appending the same amount of
#
characters before the starting quotes and after the ending quotes.For example, if we want to paste the literal string
1 + 2 = \n \(1 + 2)
, we could write it as:#"1 + 2 = \n \(1 + 2)"#
.Escape sequences can still be used, using the specified amount of
#
characters for the string. For example,###"Hello,\###nWorld!"###
becomes:This works for both single-line, and multi-line strings.
We could change the way escape sequences would be specified or simply changle the
#
character. The simplicity of this method seems quite elegant.The simplest way we could summarize the behavior, is that the number of
#
s modify the escape sequence:#
->\
is the escape sequence#
->\#
is the escape sequence##
->\##
is the escape sequence###
->\###
is the escape sequenceAnother example:
which becomes
Character literals
The single-quote character could be very valuable to us in other ways. Since character literals are not that significant, I'd like to suggest merging them in with string literals.
Since there have been discussion about prefixing the literal with the encoding used -
u8 "Hello", or u16 "Bye" for example
-, we could do the same to turn a string-literal into a character literal using thechar
prefix, as long as it actually represents a single character. For example,char "a"
would be the character literala
. String interpolation would not be allowed, as that would require runtime checks.The text was updated successfully, but these errors were encountered: