Proposal: remove std.builtin.Type.undefined
; the expression undefined
requires a result type
#22710
Labels
Milestone
RLS has reached a point where it works really nicely as the primary mechanism of type inference; so much so that we have removed anonymous struct types without significant breakage in the wild. The idea here was that anonymous struct type coercions were a legacy feature from before we used result types for type inference.
Zig has two more constructs which obviously fit this bill:
null
andundefined
.null
is unfortunately necessary (at least with how==
works today) forx == null
to work: since==
doesn't forward a result type to either operand, the RHS of==
needs to be able to stand without a result type, sonull
as a standalone expression needs to work. (This could change by changing how==
works a little, but that's out of scope for this proposal.)However,
undefined
does not match this description. Comparing any value toundefined
always results in@as(bool, undefined)
(unless the value's type is zero-bit, in which case the comparison is redundant), so it is not a useful construct. In fact, there is no context in whichundefined
is useful without a result location:a + undefined
either invokes Illegal Behavior or returnsundefined
+
,/
,>>
, etc-
,~
@divExact
,@abs
, etcanytype
parameter can only be used by checking for@TypeOf(x) == @Type(.undefined)
, which is not usefulconst
withundefined
just aliasesundefined
, which is not usefulGiven this, I am proposing the following change to the language:
undefined
always requires a known result type. It is a compile error ifundefined
does not have a known result type.@TypeOf(undefined)
is a compile error..undefined
variant is removed fromstd.builtin.Type
.This simplifies the language by removing a type tag, which benefits the language spec (one less type whose behavior must be defined!) and generic Zig code (exhaustively switching on
@typeInfo
is simpler). It may also makeundefined
a little easier to understand for beginners, since they'll never have to face the awkward concept of@Type(.undefined)
; they'll get a useful compile error as soon as they try to misuse theundefined
identifier.Note that under this proposal,
undefined
retains its status as a builtin identifier which is not a syntactic keyword.EDIT
I have thought of a realistic use case this will break:
I think code like this ought to have a type annotation for clarity anyway, so if anything, I think this is good, but it is a way in which this proposal is more breaking than I thought.
Note also that #22182 would also break this snippet if accepted.
The text was updated successfully, but these errors were encountered: