3 ms·
I much prefer Cs solution to this problem; you can write multiple literals in sequence and they will be concatenated: char* text = "hello" " dear" " sir";
by strictfp 6y ago
I much prefer Cs solution to this problem; you can write multiple literals in sequence and they will be concatenated:
char* text = "hello" " dear"
" sir";
This is great, because it doesn't make leading whitespace significant on the lines which the text block spans.
The Java approach, which seems similar to the Python approach, looks really silly when you are defining a literal in a nested scope;
def hello():
def world():
text = """hello
world"""
print text
- Normal_gaussian 6y ago> This is great, because it doesn't make leading whitespace significant on the lines which the text block spans. I think you might want to go and reread the article, they are using "determining lines" to remove leading whitespace. I think I would find this approach much more readable than Cs
- strictfp 6y agoThe thing is that this is too much magic. You end up wasting time getting your literal right. Whereas Cs solution is simple and just works.
- looperhacks 6y agoThe difference between Python and Java is that the following (Python): text = """hello world""" will contain the intendation before world, while in Java: String text = """hello world""" will be equivalent to "hello\nworld".
- deleted 6y ago[deleted]
- masklinn 6y agoFWIW the Java snippet seems to be a syntax error instead, the JEP explicitly defines the opening delimiter as three double quotes, any amount of whitespace, and a line terminator. The closing delimiter can, however, be on the same line as the final content (in order to suppress the trailing newline for instance).
- ygra 6y agoThe latter is not valid Java, as the """ must not be followed by string contents. And var text = """ hello world"""; will not have any indentation, as the common indentation is stripped from all lines.
- apk-d 6y agoI wish that's how C# handled it! There are verbatim string literals that support newlines, but in practice, most of the time I see people stick to regular string concatenation because it ruins code indentation/alignment. That's how Rider chooses to refactor code when you insert a newline as well. Multi-line verbatim strings begin to get useful around the point where you should be probably loading your text from some resource instead of embedding it in the source.
- ygra 6y agoThere's plenty of contexts where the extra indentation doesn't matter, though. And C#'s verbatim strings, at least when it comes to multiple lines, have the benefit of being extremely predictable in what ends up in the string (everything, including whitespace). They also have other uses that Java doesn't even address, like not interpreting escape sequences inside the string. I'm not sure you can really compare them. They're two different features with fairly different uses, it's just that one or two of those uses somewhat overlap.
- apk-d 6y agoThey're comparable in the sense that I'm guessing C# is unlikely to get yet another string declaration syntax, so we're stuck with what we've got. Regarding predictability I definitely agree, but good syntax highlighting would help with that.
- bootlooped 6y agoAbout 25% of the way through the article they address this with an example of XML in a text block. You can have the text justified with the level of indentation where you declared it, and not have that indentation carry over into the string. Did you read that part?
- ronnier 6y agoRead the article again. Java wouldn’t need to be aligned like your python example at all. You can align it up nice and neat with world aligned with hello.
- mohaine 6y agoIt would actually be this, according to the article. void m() { System.out.println(""" hello world """); }
- masklinn 6y ago> I much prefer Cs solution to this problem; you can write multiple literals in sequence and they will be concatenated: Python also allows that. The semantics are completely different and not at all incompatible (even though Java doesn't support implicit concatenation of sibling literals), string blocks means you don't have to bother with explicit newlines and are way more convenient for long blocks of embedded text, especially when newlines are significant and frequent (e.g. wrapped paragraphs). Java also seems to be getting a slightly more flexible version of Swift's processing, so it will automatically dedent based on the indentation of the line containing the trailing delimiter, or the least indented line of content within the block. Meaning you can write text = """ hello world""" and get "hello\nworld". Though it is apparently forbidden to have non-whitespace content on the same line as the opening delimiter: > The opening delimiter is a sequence of three double quote characters (""") followed by zero or more white spaces followed by a line terminator.
- Too 6y agoMultiple string literals in sequence is the worst feature ever. If i had a penny for every time someone forgot a comma in a list of strings... ["foo", "bar" "buzz"]