News for hackers. Stuff that matters.
Re:Zig's dot star syntax (value.*) by IshKebab on Saturday March 08, 2025 @05:05PM (#43303970)
Same reason Rust uses foo.await instead of await foo. It's clearly superior syntax.

The whole point of Zig is to fix C's mistakes. I don't know why they'd repeat this one.

Re:Zig's dot star syntax (value.*) by dcsommer on Saturday March 08, 2025 @05:47PM (#43304279)
I think Rust could use some syntax improvements for pointer manipulation. It has suffix .await, so maybe the community is ready for suffix .* for dereference now too. Visually scanning left and right, along with the extra parentheses, makes pointer-based code in Rust worse than C++ almost. A fully left-to-right syntax would be amazing. EDIT: found https://github.com/rust-lang/rust/issues/10011 and https://github.com/rust-lang/rfcs/pull/3577
Re:Zig's dot star syntax (value.*) by moomin on Saturday March 08, 2025 @05:27PM (#43304154)
I’ve gotta say, when I first learned Rust had gone its own way on await, I was heavily sceptical. But seeing the actual examples was pretty compelling.
Re:Zig's dot star syntax (value.*) by IshKebab on Sunday March 09, 2025 @09:43AM (#43309016)
Yeah I was skeptical too but it makes so much more sense. Now I'm constantly thinking "this is dumb" when using other languages with the "normal" syntax.

I wish they'd got it right for (de)referencing too though.

Re:Zig's dot star syntax (value.*) by FlyingSnake on Saturday March 08, 2025 @06:35PM (#43304607)
At this point &var and *var are pretty much an established way across languages, like if-else. I don’t see any benefit of reinventing this paradigm.
Re:Zig's dot star syntax (value.*) by epolanski on Sunday March 09, 2025 @04:50AM (#43307385)
The fact that the author of Zig who's extremely well versed in C and C++ went this way should make you at least try to think why he went that way before dismissing it.
Re:Zig's dot star syntax (value.*) by FlyingSnake on Sunday March 09, 2025 @05:14PM (#43313911)
Sounds like Appeal to Authority fallacy.
Re:Zig's dot star syntax (value.*) by xigoi on Sunday March 09, 2025 @05:50AM (#43307675)
> Copying bad design is not good design.

—Andreas Rumpf

Re:Zig's dot star syntax (value.*) by nineteen96 on Sunday March 09, 2025 @06:18AM (#43307804)
Agreed. And p.* is remarkably unintuitive and nonsensical compared to *p.
Re:Zig's dot star syntax (value.*) by int_19h on Sunday March 09, 2025 @07:08AM (#43307995)
It's remarkably intuitive and sensible if you remember that . in Zig auto-dereferences for field access and then treat `*` in this construct as field name denoting the entire object.

Ada does the same exact thing, except there you write `p.all` instead.

In any case, while the exact syntax may not be ideal, using a postfix operator for dereferencing is vastly better than prefix in practice due to typical patterns of use. There's a reason why you end up writing () a lot in C code with heavy pointer operations - the things they end up mixed with most often turn out to have the wrong kind of priority and/or precedence more often than not. Things are much simpler when everything is postfix and code reads naturally left-to-right.

Re:Zig's dot star syntax (value.*) by IshKebab on Sunday March 09, 2025 @09:44AM (#43309020)
It's not at all unintuitive of nonsensical. You're just really used to *p because it what C uses.
Re:Zig's dot star syntax (value.*) by wruza on Sunday March 09, 2025 @07:31AM (#43308120)
You mean like if-else if, if-elseif, if-elsif, if-elif? I think I should use eif for my next language. You can have elsf and elf.

It’s not that important though. Many people write two or more languages without mixing the syntax.

p.* and lv.& would be good post-operators, imo. Less going left and parenthesizing.

Re:Zig's dot star syntax (value.*) by FlyingSnake on Sunday March 09, 2025 @05:18PM (#43313966)
But those are all variations of if-else, right?

The alternative to if-else would be something like `assuming-otherwise`.

The point was that there wasn’t really a pressing need to reinvent the *p/&p syntax.

Re:Zig's dot star syntax (value.*) by throwawaymaths on Saturday March 08, 2025 @06:11PM (#43304460)
why is foo.await better?

is it a method call? is it a property/field value? how am i to be indicated that there is an implicit suspend point when you .await?

Re:Zig's dot star syntax (value.*) by vvillena on Saturday March 08, 2025 @07:32PM (#43304960)
You will know because any text editor or IDE worth the name will light up that ".await" in such a way you will immediately know it's not a method call or a struct field. The entire construct, including the dot, is a postfix keyword.
Re:Zig's dot star syntax (value.*) by dcsommer on Saturday March 08, 2025 @06:19PM (#43304514)
It preserves a left to right reading of the code. You don't have to jump visually elsewhere.
Re:Zig's dot star syntax (value.*) by rezonant on Sunday March 09, 2025 @03:20AM (#43306976)
It's the difference between (await (await foo).bar).baz and foo.await.bar.await.baz -- which do you want to write?
Re:Zig's dot star syntax (value.*) by int_19h on Sunday March 09, 2025 @07:24AM (#43308085)
In general, the rule of thumb is that prefix and postfix operators don't mix well in a single expression - order of operations is confusing, and reading the code requires going back and forth to follow it.

In the ideal world, we wouldn't have unary prefix operators at all, but unfortunately unary +, -, and NOT are prefix mostly because they were that in math notation and got grandfathered in (bonus points to Smalltalk here for going with consistency here - "not" and "negated" are regular nullary methods there so it's postfix throughout!). However, these are rarely themselves an operand of another unary operator, so you can mostly deal with this by giving postfix higher precedence than prefix in all cases, so at least it's a simple rule. But then for pointer dereference, it is in fact common to have the result of a dereference itself be an operand in the middle of another expression.

So now you have some choices to make. If you make the pointer dereference prefix, then you don't need extra parentheses when applying other prefix operators to it, e.g. -*p or !*p. If you make it postfix, then you don't need extra parentheses when applying other postfix operators to it, e.g. (using Pascal-style ^ for dereferencing) p^[0] or p^.x. Alternatively, you could add special postfix operators that desugar into the combination but avoid those extra parens, which is what C did with -> for field access.

(Technically, you could also make everything prefix instead, e.g. field access ALGOL 68 style: `month OF birthDate OF person`. But this is very counter-intuitive with indexing, and also makes code completion unusable, so it's not a serious option.)

Re:Zig's dot star syntax (value.*) by wruza on Sunday March 09, 2025 @07:56AM (#43308294)
Is this something you write often in Rust? A promise returning a struct with a promise returning a struct with a promise.

Although I agree that await could use some precedence like new in javascript:

  new types.Date().toJSON()
  // fine

  await readdir(…).filter(…)
  // oops
I’d rather parenthesize what I’m awaiting in complex cases than awaiting just everything that follows.
Re:Zig's dot star syntax (value.*) by spacedcowboy on Sunday March 09, 2025 @01:09PM (#43311148)
> which do you want to write?

Neither, honestly. They both look terrible.

Re:Zig's dot star syntax (value.*) by d0mine on Sunday March 09, 2025 @04:44PM (#43313578)
In Python, there is no .await. But I can't remember seeing (await (await for).bar) ever--meaning it is very unlikely. It is usually written as:

    bar = await foo()
    await bar.coro(*args)
Some people hate that calling functions of different colors is syntactically distinct. On the contrary, I find it beneficial that suspension points stand out. Unlike code with preemptive threads that is much harder to reason about.
Re:Zig's dot star syntax (value.*) by IshKebab on Sunday March 09, 2025 @10:20AM (#43309218)
await is a keyword so it can't be a field. Same way you know if(foo) isn't a function call.
Re:Zig's dot star syntax (value.*) by 3836293648 on Saturday March 08, 2025 @09:22PM (#43305581)
Postfix is only superior when you have lots of things to chain. C doesn't have any chaining at all. f(g(x)) is just as good as f(g(x))
Re:Zig's dot star syntax (value.*) by card_zero on Sunday March 09, 2025 @04:29AM (#43307279)
Gotta escape those asterisks so you don't output italics.

f(*g(*x)) is just as good as f(g(x*)*)

Hmm, function names go before parentheses, that looks like a good reason for dereferencing to be prefix too, otherwise they end up a long way apart.

But in the back of mind I'm thinking "semantics cause pointless fussy arguments, all code is ugly, let's stop programming in text somehow".

Re:Zig's dot star syntax (value.*) by 3836293648 on Sunday March 09, 2025 @06:17AM (#43307798)
Oh yeah, forgot HN supported some markdown.

But this is literally not semantics. Semantics is everything of value, this is just syntax.

Re:Zig's dot star syntax (value.*) by card_zero on Sunday March 09, 2025 @08:07AM (#43308376)
Sorry, yes, syntax. I was equating it to "purely semantic argument", arguing about the meaning of words. Here about it's where to put symbols.
Re:Zig's dot star syntax (value.*) by dfawcus on Sunday March 09, 2025 @06:49AM (#43307904)
One is able to chain calls in C via:

    x()->g()->f();
At least one person at Bell Labs historically used that scheme for some graphics program.

All it requires is that the function returns a pointer to a struct which itself contains function pointers.

C also allows the dot form if you really want it:

    x().g().f()
Simply by returning structs, and for all compilers that I know if, such end up using a hidden pointers to structs.

Now to make it a bit more usable, one needs a bit of planning so that either of these can be done:

    x(&x_out).g(&g_out, x_out).f(&f_out, g_out);

    x(&x_out)->g(&g_out, x_out)->f(&f_out, g_out);
(I'd suggest the latter is now the more readable of the two).

Where there is a VFT in each of the xxx_out structures, and the calls simply returns the VFT, while the whole abstraction is stored/returned via the out arguments.