thomas.bockman at gmail.com
Tue Apr 21 18:09:59 UTC 2020
On Tuesday, 21 April 2020 at 16:03:20 UTC, Russel Winder wrote:
> then which of these is the right way of accessing the value?
Either is acceptable because there is no way that this operation
can fail. Using a naked `cast` makes less work for the compiler
and performs optimally with or without inlining, though.
> conversely what is the right way of going the other way:
Use `to` except where you can gaurantee that the input value maps
to a valid enum member, because `cast` does not check your work:
writeln(cast(ZoneNumber)17); // breaks the type system
writeln(to!ZoneNumber(17)); // throws a ConvException: Value
(17) does not match any member value of enum 'ZoneNumber'
So, `cast` is faster, but unsafe. `to` is slower, but protects
the enum's invariant.
More information about the Digitalmars-d-learn