Stream: beginners

Topic: Enforcing signedness


view this post on Zulip jan kili (Mar 14 2022 at 09:50):

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?

view this post on Zulip Ayaz Hafiz (Mar 14 2022 at 12:40):

with abilities it could be possible

view this post on Zulip Richard Feldman (Mar 14 2022 at 13:29):

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

view this post on Zulip Richard Feldman (Mar 14 2022 at 13:29):

amusingly, that would also improve negation performance because it would mean we no longer had to do a conditional to check for overflow there

view this post on Zulip Richard Feldman (Mar 14 2022 at 13:30):

(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!)

view this post on Zulip Richard Feldman (Mar 14 2022 at 13:31):

so we could have a SignedArithmetic ability, yeah

view this post on Zulip Brendan Hansknecht (Mar 14 2022 at 15:35):

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.

view this post on Zulip Richard Feldman (Mar 14 2022 at 16:47):

oh true, for integers anyway

view this post on Zulip Richard Feldman (Mar 14 2022 at 16:47):

not floats, but I guess we already don't need it there


Last updated: Jul 06 2025 at 12:14 UTC