-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove void{}
from the language
#15213
Comments
I've been using it (in "real code" no less), but I'll stop now. |
I can find at least 190 real uses of That being said, makes sense to remove it imo (and it's easy to find-and-replace) just to play devil's advocate: which code snippet is easier to read? std.sort.sort(
WatchItemIndex,
evict_list[0..evict_list_i],
void{},
std.sort.desc(WatchItemIndex),
); std.sort.sort(
WatchItemIndex,
evict_list[0..evict_list_i],
{},
std.sort.desc(WatchItemIndex),
); |
i generally support this proposal but i will add that fwiw |
It doesn't really matter how grepable it is if nobody uses it. |
More to the point, when would you ever need to grep for void constants? |
I find the second snippet easier to read (in the sense that I understand it faster); when I was less experienced with Zig, I suspect I would have had no strong preference. |
I would argue in favor of removing
The first one. |
As addressed in the original issue post, this syntax is inconsistent with the rest of the language, since
How is
As I previously asked: why does this actually matter? When do you need to grep your codebase for |
@sigod The syntax arises naturally from being an empty unlabeled block const std = @import("std");
const a: u32 = 8;
const b: u32 = b: { // constructed using a block instead of just a literal
// arbitrary statements
std.debug.assert(10 > 5);
break :b 8;
};
const c: void = c: { // now we provide `void` instead of `u8` as value
std.debug.assert(10 > 5);
break :c void{};
};
const d: void = { // providing no value implicitly provides a void value
std.debug.assert(10 > 5);
// comparable to how `void` functions aren't required to have a `return` statement
};
const e: void = {}; // same as above, but empty (has no statements)
const f = {}; //same as e, but type `void` is deduced And as I've pointed out before, if you prefer the syntax pub const Void = struct{}; // use this instead of `void` and now you _have to use `Void{}` instead of `{}` everywhere! |
Using
As a zero sized type, void is isomorphic to all other zero sized types, many of which are composite types: Of course, this analogy isn't quite perfect: u0, for example is also a zero sized type, and isn't a composite type. However, I think the analogy makes enough sense for void{} to make sense. |
TLDR
void{}
is unused in all real codevoid{}
is a pointless second way of achieving the same thing as{}
void{}
is a special case which preventsT{}
syntax from being for aggregate initialization onlyvoid{}
should therefore be removed from the language.Not Long Enough, Let Me Read
Currently, there are two ways to write a
void
literal:{}
, andvoid{}
.{}
is the canonical form: it's explicitly recommended overvoid{}
, works in every casevoid{}
does, and is used in all Zig code I've ever seen. The formvoid{}
does not exist in the entire compiler and standard library, and I've never seen it used in the wild. There seems to be no practical reason for it to exist.Despite its lack of use, I'd argue its existence still violates the Zen of
Only one obvious way to do things
. To someone not explicitly familiar with Zig's conventions, it's not at all clear which is preferable: maybe explicitness is preferred?The form
void{}
also leads to an inconsistency regarding initialization syntax. In general, it is a rule that.{}
is an aggregate initializer, not a general type initializer (you can't initialize au32
, a*T
, etc with it). That's why, in stage2, it was made that.{}
is no longer a valid initializer forvoid
values. However, in every other case in the language, it is the case that.{}
is precisely equivalent toT{}
whereT
is the type being initialized; therefore, we can also say thatT{}
for anyT
is aggregate initialization syntax. Sincevoid
is not an aggregate, maintaining thevoid{}
syntax is inconsistent, particularly with the removal of.{}
initialization forvoid
(which is, for the record, a decision I agree with).The
void{}
form should be eliminated from the language. Azig fmt
fixup would be nice, but honestly isn't even really necessary IMO since I've genuinely never seen anyone use this form.The text was updated successfully, but these errors were encountered: