Live data from Hacker News

Zig's Lovely Syntax

matklad.github.io

171–180 of 246 posts

Re: Zig's Lovely Syntax

#171
post #158

I wish Zig had lovely vector, quaternion, matrix etx syntax. The team's refusal to add operator overloading will prevent this.

Agreed, their reason for not allowing it is weird. No hidden overloading? OK make it explicit then: #+, #/ would be fine.

That would open a can of worms, because then the next thing would use a different symbol. AFAIK, Scala had a huge issue with random symbols polluting code readability.

Re: Zig's Lovely Syntax

#172
post #163

> as name of the type, I think I like void more than () It's the wrong name though. In type theory, (), the type with one member, is traditionally called "Unit", while "Void" is the uninhabited type. Void is the return type of e.g. abort.

If I understood `abort` semantics correctly, it has a type of `never` or Rust's `!`. Which has a meaning "unobtainable value because control flow went somwhere else". `void` is closer to `unit` or `()` because it's the type with no allowed values.

Cool trick: some languages (e.g. TypeScript) allow `void` generics making parameters of that type optional.

Re: Zig's Lovely Syntax

#174
post #54

> 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.

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.

Re: Zig's Lovely Syntax

#175
post #163

> as name of the type, I think I like void more than () It's the wrong name though. In type theory, (), the type with one member, is traditionally called "Unit", while "Void" is the uninhabited type. Void is the return type of e.g. abort.

[deleted]

Re: Zig's Lovely Syntax

#176
post #163

> as name of the type, I think I like void more than () It's the wrong name though. In type theory, (), the type with one member, is traditionally called "Unit", while "Void" is the uninhabited type. Void is the return type of e.g. abort.

It fulfills the same role as C and C++'s void type. I don't think most systems programmers care about type theorist bikeshedding about purity with formal theory.

A ton of people coming to Zig are going to be coming from C and C++. void is fine.

Re: Zig's Lovely Syntax

#177
post #124

Earlier quoted context omitted.

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 i…

I may be missing something but come Go has a simple: `A simple formatted string ` ?

The problem is that usually you have something like

  fn main() {
    if something {
      print(`A
  simple
  formatted
  string`)
    }
  }
which looks ugly and confusing.

Re: Zig's Lovely Syntax

#178
post #54

> 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.

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

Upvoting because similar comments here suggest that you are not alone.

People are having trouble distinguishing between '//' and '\\'.

Re: Zig's Lovely Syntax

#179
post #124

Earlier quoted context omitted.

I may be missing something but come Go has a simple: `A simple formatted string ` ?

Yours is rendered as: A\n\tsimple\n\t\tformatted\n\t\t\tstring\n\t If you wanted it without the additional indentation, you’d need to use a function to strip that out. Typescript has dedent which goes in front of the template string, for example. I guess in Zig that’s not necessary which is nice.

[deleted]

Re: Zig's Lovely Syntax

#180
post #89
post #79

Earlier quoted context omitted.

> it's even worse that they landed on // for the syntax .. it is using \\

I worked with browsers since before most people knew what a browser was and it will never cease to amaze me how often people confuse slash and backslash, / and \ It’s some sort of mental glitch that a number of people fall into and I have absolutely no idea why.

I doubt those very people would confuse the two when presented with both next to each other: / \ / \. The issue is, they're not characters used day-to-day so few people have made the association that the slash is the one going this way / and not the one going the other way \. They may not even be aware that both exist, and just pick the first slash-like symbol they see on their keyboards without looking further.
Post reply on HN