Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting intrinsics and traits #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Casting intrinsics and traits #181

wants to merge 1 commit into from

Conversation

evincarofautumn
Copy link
Owner

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

  • A load of intrinsics

  • widen_cast for widening conversions

  • narrow_cast for narrowing conversions (returns Optional)

  • signed_cast for signedness conversions (returns Optional)

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
Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant