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
better multiline string syntax #162
Comments
2 things we didn't consider yet:
const multiline_null_term_string =
c"one\n" ++
c"two\n" ++
c"three\n" ++
c"four\n"; It's tempting to say, well then, that's what you get for using C. But C isn't going away for a long time, and part of Zig's strategy is to make Zig better at writing C code than C is at writing C code. So we should probably have a notation for C multiline strings. Here's one proposal: const multiline_null_term_string =
c\\one
c\\two
c\\three
; Now, point 2 that we didn't consider yet:
const re = compile_regex(r"#(".*"/aaa)#"); Our new syntax proposal is maybe suboptimal for this use case: const re = compile_regex(
\\".*"/aaa
; Or maybe not. It takes 3 lines instead of 1, but maybe that's actually reasonable. One constraint that I feel comfortable stating is that we should not have both of these multiline string syntaxes. It's already enough that we have single-line strings that support escapes as well as multiline strings with no escapes, so let's keep it to 2, and not 3. |
I think this is so much harder to read over all these extra chars, not copy pasteable as easily anymore and the main reason seems to be to make the job of the compiler/parser easier which is quite unimportant in my opinion. Now you better pray that your text editor has a column selection mode or this is going to be all kinds of painful. Or you better have perfect ziglang support for a shortcut in your IDE to un-multiline them all on one go. |
Yeah you should have a column mode in your editor. This isn't just about readability for a compiler. This also makes it easier for humans to read, especially when it's a very large string literal, say over 100 lines long. But the biggest win for this syntax is that indentation inside the string is decoupled from indentation outside the string. |
This design decision is locked in, stable, and will not change. It's one of the syntax decisions that I'm most happy with about Zig. |
Okay I'm sad to hear that because I can't disagree strongly enough. More characters is almost always worse in my opinion, your brain has to parse more even if it's just subconsciously. Saying that it's easier to read makes no sense to me, especially when it's a large text because then it's even more obvious that it's an external non-zig content... without the |
I strongly disagree, this is the only multiline string literal syntax that I've encountered where dealing with indentation levels is not a PITA. |
I like the multi-line syntax a lot as well. And text editors can be configured to automatically propagate |
@RUSshy Does
Status quo multiline string syntax imo handles this better than many other languages because zig doesn't allow escapes in multiline strings. const example_script = `
if(true) {
console.log("newline: \\n, backslash: \\\\");
const embedded_multiline_string = \`\`;
}
`
const newlines_with_backslashes = `\\something;
\\something_else;` const example_script =
\\if(true) {
\\ console.log("newline: \n, backslash: \\");
\\ const embedded_multiline_string = ``;
\\}
;
const newlines_with_backslashes =
\\\something;
\\\something_else;
; In zig, pasting text into a multiline string requires inserting
const example_script =
\nconsole.log("newline: \n, backslash: \\");
\nconst embedded_multiline_string = ``;
; Without syntax highlighting, this is a bit unreadable. Also, it implies there is a newline before the top line when there isn't. |
The clear solution is to use an up-high-line escape |
After discussing with @thejoshwolfe, I believe we can do even better than status quo multiline strings (See #76) which are C++ style.
This proposal is to remove the existing multiline string support and replace it with a new syntax.
Old syntax, with an example taken from tetris:
New syntax proposal:
Simpler example:
This is equivalent to:
const abc = "one\ntwo\nthree"
; Note that the idiom of adding++ "\n"
may become common practice.Rationale:
\\
looks similar to//
which indicates a comment to the end of the line, just as\\
indicates a part of a multiline string to the end of the line.The text was updated successfully, but these errors were encountered: