Can signedness/unsignedness be enforced?
I'm implementing a size-agnostic, unsigned-specific algorithm here (the linear congruential generator algorithm for pseudorandom number generation, which repeatedly multiplies and intentionally overflow-wraps). I'd like to be able to specify unsignedness in type aliases (and opaque types), but I don't think that's possible since it's currently implemented in the built-ins as the private tag @Unsigned
.
Something like this would be nice:
(I have no syntax preferences, the first line is just a guess)
MustBeUnsignedForSomeReason uint : Int ( Unsigned uint )
foo : MustBeUnsignedForSomeReason U16
foo = baz
bar : MustBeUnsignedForSomeReason *
bar = baz
baz : U16
baz = 42
Is this weird? Does Roc intentionally not support size-agnostic signedness specificity?
with abilities it could be possible
I actually thought about this at some point for purposes of making it a compile error to attempt to negate an unsigned integer, since that will always panic
amusingly, that would also improve negation performance because it would mean we no longer had to do a conditional to check for overflow there
(well, negating an unsigned integer of 0 would work, but if you're sure it's 0, why bother negating a 0? And if you aren't sure whether it's 0, then negating it isn't safe!)
so we could have a SignedArithmetic
ability, yeah
Would you still want the check due to not being able to negate -128 : I8
. Only matters for the minimum of each signed type, but still leads to an overflow case.
oh true, for integers anyway
not floats, but I guess we already don't need it there
Last updated: Jul 06 2025 at 12:14 UTC