-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Making #
free
#3498
Comments
I've to mention that |
See here for the rationale for using |
It's worth bearing in mind here, proposal #1360 adopted To note a few risks of
|
If Carbon uses
In other languages such as shell script (that's well known), |
What advantages
What advantages C++-style raw string literal has over repeated quotes?
Carbon can use a modified/enhanced C++-style raw string literals for readability. We can put symbols and characters between
Although it may seems over complicated rules, and Carbon may simply ban numbers and letters from them: // ERROR! opening and closing quotes cannot contain letters and numbers.
let x: auto = 'ab"text"ab';
// OK.
let x: auto = '(*"text"*)'; Alternatively,
But IMO they look better when let x: auto = '(*"item(*\n*)next"*)'; BTW it depends on compiler implementations and somehow it maybe is opinion-based. |
This limitation has a great advantage, it allows syntactically to put keywords within opening quote. For example:
White-space after Another example for the keyword in string quotes, is if we want to copy/paste code: var x: auto = 'code ***"python
def name():
pass
"***'; In this example, Another possible feature is template strings:
In this example, In general: var x: auto = 'KEYWORD OPENING"DATA
CONTENT
"CLOSING';
These examples demonstrate how this feature can be expanded in the future. Thanks. |
Alternative Syntax for Multiline String LiteralsAnother alternative way to have multiline is using var x: auto = ''"comment
text
"'';
var y: auto = ''^(*"comment
text
"*)^'';
var z: auto = ''OPENING"comment
text
"CLOSING''; This works because The advantage of this approach is that it's simple. It doesn't introduce keywords inside string literals. And if it's necessary, string keywords can be introduced any time without any syntax limitation or compatibility problem (because numbers and letters may be banned in |
Visually comparing
|
Yes; proposal #199 that geoffromer linked to offers rationale for block string literals. Let me try to give you an example of why block string literals are helpful: // Before
let a: auto = #"""carbon
It uses #""" """\## delimiters.
An escape sequence looks like \\##t.
Here's an actual tab: '#\t'.
"""# This yields a string of content type
Indent is removed because as part of block string literal syntax. Content type ( Also, a couple syntax corrections where I think you might've misunderstood the syntax:
Similar to other raw string literal syntax, it's looking for the close token, which is
Similar to the above, I'd expect the first example to be written with one
Note, I'm going to leave more comment to others... other than the syntax misunderstanding, it sounds like you're mostly arguing readability benefits, which I would disagree with. |
Thanks. I forgot to write // Before
let a: auto = #####"text "####" text"#####; And it requires 5
Infact I wanted to share how escape sequences can be unreadable as |
Sorry I don't get (understand) this part. But if I understand correctly, I suggest to ban numbers and letter between // ERROR!
var x: auto = 'carbon"carbon\ncarbon"carbon';
// OK.
var y: auto = '~<*"~<*\n*>~"*>~';
// y == "\n"
Additionally, I'm arguing to make |
I have to mention that |
While we understand the desire to use # in other parts of the grammar, the suggestion to use '" and "' instead introduces a more complex lexical structure that we don't think pulls its weight. We can consider # for other use cases, and if one of them is sufficiently compelling, find another way to write raw string literals. But ideally, that other way will work to preserve the simplicity of the current design -- both lexically, visually, when teaching or learning, and in composing cleanly with block string literals. There are a number of other issues somewhat raised here, but it doesn't seem like we can do much with them in this issue. We remain interested in having block string literals in the language, as well as raw block string literals. Other ideas (interpolated strings, etc) should be explored in discussions and eventually draft proposal documents that surface the motivating problem and proposed solution. Combined, the decision here is "no change at this point". For reference, leads issues are better suited to resolving a specific and fairly narrow question of how to move forward, proposals documents should be used to outline a significant change holistically and work through the rationale for it and alternatives. |
While we understand the desire to use # in other parts of the grammar, the suggestion to use '" and "' instead introduces a more complex lexical structure that we don't think pulls its weight. We can consider # for other use cases, and if one of them is sufficiently compelling, find another way to write raw string literals. But ideally, that other way will work to preserve the simplicity of the current design -- both lexically, visually, when teaching or learning, and in composing cleanly with block string literals. There are a number of other issues somewhat raised here, but it doesn't seem like we can do much with them in this issue. We remain interested in having block string literals in the language, as well as raw block string literals. Other ideas (interpolated strings, etc) should be explored in discussions and eventually draft proposal documents that surface the motivating problem and proposed solution. Combined, the decision here is "no change at this point". For reference, leads issues are better suited to resolving a specific and fairly narrow question of how to move forward, proposals documents should be used to outline a significant change holistically and work through the rationale for it and alternatives. |
Summary of issue:
Carbon can use
'"text"'
notation instead of#"text"#
to make#
available for other purposes.Details:
Currently
#
is used in string literals. What if we drop#
and use'
instead?'
is already used for character literals in many programming languages. If we require"
to be escaped in character literals, we can use notation'"..."'
for string literals:So Carbon reuses
'
, and#
will be free for other uses in the language.Alternative Syntax
Instead of repeating
'
s, Carbon may use a notation similar to C++ raw string literals. We can put other symbols (except'
and"
) between'
and"
in a way that they must have reversed order in ending quote (If we put one of[({<
characters in starting quote, correspondingly we have to use>})]
in the ending quote). Also escape sequence character\
must include those extra characters. So for example:IMO having notation around escape sequences, makes them readable:
\<n>next
is more readable than\##nnext
. Thanks.Any other information that you want to share?
I had suggested a similar notation (alternative syntax) for string literals in Cpp2 (Cppfront) in this discussion (Please read it for explanation and why
'"text"'
is chosen). It seems Cpp2 won't get the new notation (maybe because of compatibility with Cpp1, similar to how TypeScript is compatible with JavaScript).The text was updated successfully, but these errors were encountered: