evincarofautumn / kitten

A statically typed concatenative systems programming language.
http://kittenlang.org/
Other
1.1k stars 42 forks source link

Casting intrinsics and traits #181

Open evincarofautumn opened 7 years ago

evincarofautumn commented 7 years ago

Sketching out how this all ought to work. Currently I have:

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>, and Saturating<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 be Optional 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.

evincarofautumn commented 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).