logoalt Hacker News

z_openlast Sunday at 5:18 PM14 repliesview on HN

> Raw or multiline strings are spelled like this:

    const still_raw =
        \\const raw =
        \\    \\Roses are red
        \\    \\  Violets are blue,
        \\    \\Sugar is sweet
        \\    \\  And so are you.
        \\    \\
        \\;
        \\
    ;
This syntax seems fairly insane to me.

Replies

IshKebablast Sunday at 5:28 PM

Maybe if you've never tried formatting a traditional multiline string (e.g. in Python, C++ or Rust) before.

If it isn't obvious, the problem is that you can't indent them properly because the indentation becomes part of the string itself.

Some languages have magical "removed the indent" modes for strings (e.g. YAML) but they generally suck and just add confusion. This syntax is quite clear (at least with respect to indentation; not sure about the trailing newline - where does the string end exactly?).

show 6 replies
n42last Sunday at 5:49 PM

Zig does not really try to appeal to window shoppers. this is one of those controversial decisions that, once you become comfortable with the language by using it, you learn to appreciate.

spoken as someone who found the syntax offensive when I first learned it.

ivanjermakovlast Sunday at 9:59 PM

It is not the insane syntax, but quite insane problem to solve.

Usually, representing multiline strings within another multiline string requires lots of non-trivial escaping. This is what this example is about: no escaping and no indent nursery needed in Zig.

whitehexagonlast Sunday at 7:35 PM

I think Kotlin solves it quite nicely with the trimIndent. I seem to recall Golang was my fav, and Java my least, although I think Java also finally added support for a clean text block.

Makes cut 'n' paste embedded shader code, assembly, javascript so much easier to add, and more readable imo. For something like a regular expressions I really liked Golang's back tick 'raw string' syntax.

In Zig I find myself doing an @embedFile to avoid the '\\' pollution.

rybosomelast Sunday at 7:07 PM

Visually I dislike the \\, but I see this solves the problem of multiline literals and indentation in a handy, unambiguous way. I’m not actually aware of any other language which solves this problem without a function.

throw10920last Sunday at 5:55 PM

It seems very reasonable and comes with several technical and cognitive advantages. I think you're just having a knee-jerk emotional reaction because it's different than what you're used to, not because it's actually bad.

conacloslast Sunday at 11:26 PM

I like the idea of repeating the delimiter on every line. However `//` looks like a comment to me. I could simply choose double quote:

    const still_raw =
        "const raw =
        "    "Roses are red
        "    "  Violets are blue,
        "    "Sugar is sweet
        "    "  And so are you.
        "    "
        ";
        "
    ;
This cannot be confused with a string literal because a string literal cannot contain newline feeds.
show 1 reply
hardwaregeeklast Sunday at 5:44 PM

My immediate thought was hmm, that's weird but pretty nice. The indentation problem indeed sucks and with a halfway decent syntax highlighter you can probably de-emphasize the `//` and make it less visually cluttered.

conorberginlast Sunday at 6:04 PM

I think everyone has this reaction until they start using it, then it makes perfect sense, especially when using editors that have multiple cursors and can operate on selections.

seabombslast Monday at 1:19 AM

I think the syntax highlighting for this could make it more readable. Make the leading `\\` a different color to the string content.

zemlast Monday at 9:27 AM

that was my favourite bit in the entire post - the one place where zig has unambiguously one-upped other languages. the problems it is solving are:

1. from the user's point of view, you can now have multiline string literals that are properly indented based on their surrounding source code, without the leading spaces being treated as part of the string

2. from an implementation point of view having them parsed as individual lines is very elegant, it makes newline characters in the code unambiguous and context independent. they always break up tokens in the code, regardless of whether they are in a string literal or not.

steveklabniklast Sunday at 5:55 PM

I had the exact opposite reaction.

klas_segeljaktlast Sunday at 6:07 PM

When I first read it I thought it was line comments.

show 1 reply
fcourylast Sunday at 5:24 PM

I really like zig but that syntax is indeed insane.