'\'' should not exist

In Zig, '' is illegal. So there is no reason for a literal ' to be spelled '\'' (which is hideous), when it could be spelled with the also-illegal construct '''. Which is entirely preferable to the spurious backslash.

Bonus: '\', not '\\', which is also not-very-nice. I know we’re all used to it… but what if we’re too used to it?

7 Likes

There could be one release where both forms are legal, and zig fmt rewrites them to use the preferred form. The transition would be almost entirely without pain.

We don’t have to live like this!

2 Likes

Counterpoint: '\n', '\x12', '\u{1234}'

Character literals are not limited to u8, and the escapes being consistent with string literals is a nice property IMO.

5 Likes

I’m not saying we don’t need backslash escapes.

I’m just saying that a literal ' can be ''' and a literal backslash can be '\'. We could do '\"' too, but we don’t, we do '"', because there’s no reason for the backslash.

As there is no reason for the backslash with ' and \ in character literals.

1 Like

Consistency is a reason. Might not be a compelling one, but it’s a reason.

I do get your point, though.

(' doesn’t need to be escape in string literals, for what it’s worth)

8 Likes
"'"[0]

:slight_smile:

7 Likes

I would honestly prefer to look at this. I’m not going to start doing it but it’s tempting.

I am aware that what I’m saying makes too much sense to be taken seriously. Perhaps that why I’m saying it.

2 Likes
const @"'" = '\'';

2 symbols shorter :skull:

6 Likes

I kind of prefer the explicit escape, feels more in line with everything else and not creating a special case.

4 Likes

I was hoping it’s about the Windows path delimiter, because there the backslash shouldn’t exist :wink:

(std.fs.path still has support for separate Windows path delimiters, but those haven’t been needed since at least the late 90s, the forward slash works just fine - the backslash was really just a workaround on MS-DOS because the forward slash was already taken to identify cmdline args, but the filesystem functions themselves always accepted both path delimiters).

I don’t have much of an opinion on backslash as the escape character, or double backslash to escape a backslash, but usually the double backslash is used in Windows filepaths, where it’s not even needed.

I have bad news about ntdll functions / NT paths :upside_down_face:

3 Likes

NT paths require \ but anything else on Windows (Win32API, C runtime library and .Net) support both \ and /.
That support dates back to the days of DOS 2.0, which supported directories for the first time, changing its API from CP/M file control blocks to Xenix (Microsoft’s Unix) file descriptors.

3 Likes

It depends on if you’re pattern matching or rule-matching, I expect.

The rule would be “backslash escapes are for escaping. Escaping is for characters you would otherwise not be able to type, or to ‘escape’ out of the string syntax into the literal syntax”. In which case I’m proposing the elimination of the two exceptions to that rule.

Pattern matching would be “it should look like every other language, I’m comfortable here don’t bother me”, in which case good news! It does. There’s a reason for it in those other languages though.

Or, to steelman the sentiment, it should be “strings and character literals should work the same”, yet I’ve never seen anyone complain that we don’t write "\'this" or this '\"'. Curious.

Status-quo bias is quite real. It’s bad, but it’s real.

I think this is a very good reason. It makes it easier to learn the language with the consistency. Coming from TS/JS or Python (and probably many others), you expect that if you start it with the same quote type, you will need to escape it.
Plus, seeing ''' would really be confusing since many languages use that for multiline strings. One would expect to see a closing ''' later.

Initially I kinda liked @mnemnion 's idea, but the more I thought about it, the more it seems like an ill-fated spend of the novelty budget. So we are saving a character or two when typing, but what do we really get from it? You read code more than you write it, so optimize for what people expect, not saving very few keystrokes.

2 Likes

Good point. Everyone edits without syntax highlighting and a language server, so no one would immediately see what was going on, adjust their habits, and enjoy a better quality of living.

The status quo is awesome, because languages look like Javascript, and that’s Good.

I have a counter proposal. There’s no need for ''' to exist at all. If '' is otherwise illegal, then let’s just make that the new '\''. Just imagine the disk space savings.

I’ll also note that there already is a shorter way of writing '\'': 39. It’s arguably clearer too. It really indicates intent (that intent being: fuck you, reader), and the programmer doesn’t have to retain the knowledge that Zig source files are UTF-8 encoded.

2 Likes

Being dismissive of the point doesn’t help your argument. Being consistent with expectations is a real inertia, and overcoming that inertia requires some good value.

Or, to steelman the sentiment, it should be “strings and character literals should work the same”, yet I’ve never seen anyone complain that we don’t write “'this” or this ‘"’. Curious.

I actually don’t think this is quite right. In languages like Python/JS there are no character literals, just strings. So you have two ways to write strings, with a single quote or double quote. If you use a single quote, you have to escape interior single quotes.

Moving to Zig where ' and " are different tokens, I think it makes sense that you have to escape it in a character literal. That matches the pattern you see everywhere else.

However I do think it is interesting that it isn’t actually needed from a tokenization standpoint. Similar to how ; aren’t actually needed by the compiler (in some languages, i think C is one), but are still required.

7 Likes

For what it’s worth, I would have never guessed that you need an escape backslash. I would have tried ''' first and then spent some time on the internet trying to figure out what’s going on.

Well, it looks like Zig is just doing the same thing as C, as you also need to escape the apostrophe there. So I guess the backlash should stay to keep it easy for programmers of one language to work with the other.

2 Likes

For that matter the closing quote isn’t necessary at all … characters being bracketed is left over from C’s support of multi-character character literals (e.g., ‘ab’ packed both chars into a 16-bit int back then). ’ (that is, quote space) would be visually confusing, but one could have \s for a space so it’s '\s. There’s precedent for this: lisp uses #\" (# backslash quote–I had to double the backslash for display) for example (or ?\" in emacs lisp) … and #\Space for ’ '.

1 Like