19 ms·
It allows you to put double quotes in the string without escaping them, which is pretty nice.
by itsibitzi 5y ago
It allows you to put double quotes in the string without escaping them, which is pretty nice.
- amelius 5y agoWhat if you want both? E.g., how would you put the following sentence in a string: You can use both " and ' to delimit a string.
- kangalioo 5y agoYou'd use escapes: "You can use both \" and ' to delimit a string"
- weird-eye-issue 5y agoThat was their point, you inevitably have to escape one or the other, so just pick one
- samhw 5y agoMy only disagreement is that you imply that this is no better than plain old JSON – I think it's positively _worse_. It makes it difficult to remember which of the two quote marks to escape, without checking the beginning of the string to see what quote marks were there (and this difficulty exists not only for humans but for machines, which introduces more complexity in parsers). The benefit of JSON is that it's simple, opinionated, and pretty. Putting bells and whistles on it ruins the very ethos of JSON. If you want JSON++, use XML or Jsonnet or whatever.
- weird-eye-issue 5y agoI didn't imply that, in fact my comment agrees with what you are saying. Did you reply to the wrong person?
- samhw 5y agoHaha, I only meant that as a "I agree with you, but I'd go even further" preface. In hindsight, this being HN, I should have been more precise!
- usrusr 5y agoAn annoying half-solution that breaks down as soon as you encounter content with both. Better imho to have one way and become good at it. I think I might even prefer trailing commas to be mandatory.
- staticassertion 5y agoIt solves the problem like 99% of the time in my experience. It's very rare that I have content with both single and double quotes.
- usrusr 5y agoThat's exactly my point: 99% solutions are the worst. Every 99% solution will eventually find someone relying on it, willingly or not. And it's always someone else cleaning up the mess (or failing to find the cause).
- n_e 5y agoThere is no mess. The 99% case is a pretty string. The 1% case is a string with escaped delimiters.
- kwhitefoot 5y agoWhy does it need commas at all? What purpose do they serve?
- kevinmgranger 5y agoIt's either commas or significant whitespace. No matter what you'll find someone who hates one or the other.
- kwhitefoot 5y agoBut everyone puts the whitespace in anyway so why not just make it significant?
- 5y ago
- andix 5y agoBut then you can't include single quotes into the string any more. Same problem again. The most practical solution for this problem I saw was in F#. You can use tripple-quoted strings. let a = """The diner is called "John's", but you can't let the string end with a double quote""" https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/strings#triple-quoted-strings https://docs.microsoft.com/en-us/dotnet/fsharp/language-refe...
- conradludgate 5y agoRust has similar but it scales infinitely. They're called raw strings, "normal string can contain ' " r#" raw string can contain " ' "# r###" if for some reason you need to write "# "### Kinda like heredocs
- 0xdeadb00f 5y agoMost (newer) programming languages have a concept of multi-line string literals.
- conradludgate 5y agoMost newer languages only have 1 variation, not infinite. Go only has backtick, python only has triple quotes. Rust has #", ##", ###", ##...#"
- Rendello 5y agoMy favourite variation is from Zig: const hello_world_in_c = \\#include <stdio.h> \\ \\int main(int argc, char **argv) { \\ printf("hello world\n"); \\ return 0; \\} ; It neatly sidesteps the whole "cat-and-mouse" issue. Pair this with the fact that it only has the C++ style // comments, it means that every line can be tokenized independently. Not that that particularly matters for programmers, but I know Vim sometimes gets confused about where Python's strings end and it turns the syntax highlighting to garbage.
- 5y ago