Open OlaFosheimGrostad opened 2 years ago
What don't you like about Rust's approach?
Rust provides explicit wrapping_add
etc. functions on ordinary integer types to opt-in to wrapping arithmetic, but also provides a special generic wrapper type Wrapping<T>
that overloads the usual operators such as +
to implement wrapping operations. The same goes for clamping (saturating) and modular arithmetic, as well as other operations.
I personally like Rust's approach here. I like the ability to encode the default arithmetic behaviour into the type system (if desired) - although you seem to not like this (any reasons why?). This approach has the benefit of keeping the core language simpler - no need to add additional operators or keywords (although you'd probably use compiler intrinsics to actually implement the ops).
Rust provides explicit
wrapping_add
etc. functions on ordinary integer types to opt-in to wrapping arithmetic, but also provides a special generic wrapper typeWrapping<T>
that overloads the usual operators such as+
to implement wrapping operations. The same goes for clamping (saturating) and modular arithmetic, as well as other operations.
I am not really using Rust, so I will answer in general.
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please comment or remove the inactive
label. The long term
label can also be added for issues which are expected to take time.
This issue is labeled inactive
because the last activity was over 90 days ago.
Clamping (or "saturation") is as useful as modular arithmetics. I think it would be an improvement to C++ if Carbon provided clamping and modular (wrapping) operators using similar mechanisms. Right now the docs specify that Carbon follows C++, but that modular operators will be considered as future work. I think it would be better for correctness if modular operations don't happen by type, but is explicit. In my opinion that covers most use scenarios on modern CPUs registers with large bit-widths.
So there are two ways to approach this:
Examples for operators:
Examples for expressions (not the best syntax):
Clamping is particularly useful in SIMD operations, so I guess one would need to figure out what SIMD operations would look like to ensure consistency across builtin types.