logoalt Hacker News

tialaramexlast Sunday at 9:39 PM1 replyview on HN

Because we've said x is a constant we're obliged to specify its type. For variables we're allowed to use inference and in most cases the type can be correctly inferred, but for constants or function signatures inference is deliberately prohibited.

    const x: Rect = ....
[Note that in Zig what you've written isn't a constant, Zig takes the same attitude as C and C++ of using const to indicate an immutable rather than a constant]

Replies

flohofwoelast Monday at 8:04 AM

> Zig takes the same attitude as C and C++ of using const to indicate an immutable rather than a constant

I think it's a bit more complicated than that: AFAIK Zig consts without explicit type may be comptime_int or comptime_float, and those don't exist at runtime. Only consts with an explicit type annotation are 'runtime consts'.

Also see:

https://www.godbolt.org/z/esTr463bT

...still, I think Rust should allow to infer the type at least inside struct initialization, it would make designated init code like this a lot less noisy (Zig would suffer from the same problem if it hadn't the .{} syntax):

https://github.com/floooh/sokol-rust/blob/main/examples/texc...

...C99 is still the 'benchmark' when it comes to struct initialization:

https://github.com/floooh/sokol-samples/blob/29d5e9f4a56ae18...

show 1 reply