4 ms·
I've given some thought to this kind of string literal in the past (for my imaginary programming language). I want a syntax something like this: var xml = ""
by __ryan__ 5y ago
I've given some thought to this kind of string literal in the past (for my imaginary programming language). I want a syntax something like this:
var xml = """<element attr="content">
""" <body>
""" </body>
"""</element>
""";
This would give you the string:
<element attr="content">
<body>
</body>
</element>(no newline)
If you wanted a newline at the end, you'd do this:
var xml = """<element attr="content">
""" <body>
""" </body>
"""</element>
"""
""";
Basically the end delimiter of the string would be the last """. You could concatenate two strings like so:
var xml = """<element attr="content">
""" <body>
""" </body>
"""</element>
"""
""" // this string ended on this line
+
"""<element attr="content">
""" <body>
""" </body>
"""</element>
"""
"""; // this string ended on this line
This could use the same logic for using at least three quotes as the indicator that it's a multiline string.
Please, tear this apart and offer improvements.
Edit: this is conceptually similar to Zig's multiline literal: https://ziglang.org/documentation/master/#Multiline-String-Literals https://ziglang.org/documentation/master/#Multiline-String-L...
- wangweij 5y agoOne benefit of multiline raw string is that you can directly copy/paste a block of characters between the program and its source. Unless there are very sophisticated IDE support this proposal does not work fine in this sense.
- __ryan__ 5y agoAh, that's interesting. In general, yes my proposal does benefit from and assume some IDE/tooling support for quality of life. Edit: To be specific, the IDE could handle formatting when pasting into a line beginning with """. Or offer a "paste as cool new multiline string syntax" feature.
- deleted 5y ago[deleted]
- jcelerier 5y ago... which text editor does not have multi-line block selection these days ?
- pjob 5y agoThe browser that I'm using to read this proposal, for one.
- skrebbel 5y agoI don't understand what problem this solves that the proposal in the linked article doesn't solve. Maybe you're not suggesting that it does, but then what's the upside?
- __ryan__ 5y agoOh yeah, I was just sharing my string literal syntax that's been baking in my head for while for the sake of discussing. But off the top of my head, mainly just that there's a clear visual indicator of the start of lines of text, rather than counting/lining up leading whitespace. In the first example, the strings are all tabbed evenly for the sake of looking "pretty" in the code, but the following would generate the same string, since each line begins after the """: var xml = """<element attr="content"> """ <body> """ </body> """</element> """;
- Metasyntactic 5y agoHi. I'm the language designer behind this feature :) A few points. > but the following would generate the same string, since each line begins after the """: That's not a virtue here. The point is to be able to write clear literals that never need escapes and which allow for easy grokking of what the content actually is. All current string forms in C# require some amount of manual (or tooling) help to fix them up to be legal. That's not the case with this literal. The content can always work as-is without having to touch it at all.
- __ryan__ 5y agoThat definitely makes sense. In my (limited, especially not C#) experience I just really dislike reasoning about trimming the leading whitespace, even if the rules are simple. I yearn for a consistent visual cue. In my syntax, the IDE would ideally treat the """ block virtually like a <textarea>.
- Metasyntactic 5y ago> I just really dislike reasoning about trimming the leading whitespace Note: this feature is entirely optional. You can absolutely not have leading whitespace trimming at all. Indeed, this is a requirement of the proposal as we have to make it possible to actually represent text that has leading whitespace :)
- Banana699 5y agoXtend is a programming language designed as a syntactic layer over Java (and transpiles to it), it was designed specifically for code generation for the Xtext framework. It has its own take on multi-line interpolation-equipped strings : template expression. https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html#templates https://www.eclipse.org/xtend/documentation/203_xtend_expres... It has this idea called whitespace preprocessing, it has several rules but one of them guarantees that your first 2 examples would work as you intend them to.
- allisdust 5y agoYou should look at how string literals in Rust work. Easily customisable and can include any character.