Open evincarofautumn opened 7 years ago
Hey @strager, @lewurm, @LiraNuna, do you have any thoughts on this? I’m trying to work out a nice API for casting, and it’d be useful to have some feedback so I can land this. In particular, I’m wondering if we can reduce the number of intrinsics.
This is basically intended to cover the function of static_cast
in C++. I’m thinking of adding coerce<A, B> (A -> B +Unsafe)
with the semantics of reinterpret_cast
, implemented in terms of a couple of primitives like forget<T> (T -> +Unsafe)
(non-zeroing drop) and undefined<T> (-> T +Unsafe)
(uninitialised stack allocation).
Sketching out how this all ought to work. Currently I have:
A load of intrinsics
widen_cast
for widening conversionsnarrow_cast
for narrowing conversions (returnsOptional
)signed_cast
for signedness conversions (returnsOptional
)The intrinsics aren’t yet implemented, and this doesn’t cover coercions with wraparound/truncation. I’d like to have wrapper types like
Wrapped<T>
,Unchecked<T>
, andSaturating<T>
for different overflow behaviours—that depends on making arithmetic+Fail
by default and making+Fail
an implicit permission.Originally I intended to have a single
cast
trait, where the instances would beOptional
in the result type for narrowing conversions, and I think that might be more ergonomic, but it might also require more type annotations and/or lead to bugs because it’s not very explicit.