You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type.from_int on a discriminant enum with payload variants: what does it return? (Option[Type] cannot hold a payload variant) #1497
Type.from_int(n) on a discriminant enum whose variants carry payloads returns the repr's Option — Some(7) — because no Msg value exists for a payload variant's discriminant. The spec says from_int returns Option[Type], which a payload variant cannot satisfy. Needs a ruling.
enum Msg: i32:
Quit = 0
Move(i32, i32) = 7
fn main:
let o = Msg.from_int(7)
match o:
Some(v) => print(v)
None => print("none")
Prints 7 (o: Option[i32]).
Spec text
§4.4a: "Conversion:Type.from_int(n) converts an integer to Option[Type], returning .None for values that don't match any defined discriminant". §4.4a also allows payload variants with explicit discriminants ("Discriminant enums with payloads: Variants can carry associated data just like regular enums, combined with explicit discriminant values").
The question
For an enum with payload variants: is from_int rejected (it cannot build a payload), or Option[Type] that is Some only for payloadless variants and None for a payload variant's discriminant, or something else? The #1453 fix makes from_int an Option[Type] for fieldless discriminant enums (the spec's Color.from_int(2) example) and leaves the payload case as it was until this is ruled.
Activity
Sema: Type.from_int(n) is an Option[Type] (#1453)
Sema: Type.from_int(n) is an Option[Type] (#1453)
Sema: Type.from_int(n) is an Option[Type] (#1453) (#1541)
D71 language rulings: @[flags] without repr, from_int on unit enums, …