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
[SR-8156] Difficult to use random integers in generic contexts #50688
Comments
It's seems likely that `FixedWidthInteger` should simply require `Stride: SignedInteger` and `Magnitude: UnsignedInteger` as a basic requirement. That would happily fix this bug too. |
CC @lorentey@airspeedswift@moiseev |
That would be ideal! I don't recall if we ever tried adding specifically these requirements, but I remember running into unacceptable compiler performance regressions when we wanted to add Collection conformance to FixedWidthInteger.Words. Recursive requirements like these slowed down compilation too much. The conformance checker improved a great deal this year; it'd be worth a check to see if the issue is still there. |
I think Stride: SignedInteger is basically a no-brainer (assuming that conformance checking performance isn't an issue), but Magnitude: UnsignedInteger would proscribe implementations of FixedWidthInteger that are signed with a sign-magnitude representation, which probably want Magnitude: Self. I'm not sure if we care, however. |
Alternatively I think we could also drop the Magnitude: UnsignedInteger requirement by implementing next(upperBound: T) on FixedWidthInteger instead of FixedWidthInteger & UnsignedInteger, with precondition(upperBound > 0). |
I don't recall if swift-evolution had a discussion on the UnsignedInteger requirement; but if it was up to me, I'd allow both RandomNumberGenerator.next() and .next(upperBound🙂 to be used on signed integers, too! |
/cc @xwu and @natecook1000 |
As for the the using random in generic context, I wonder if we can change the |
@swift-ci create |
If it doesn't adversely effect performance, I think the right fix here is to add Stride: FixedWidthInteger & SignedInteger and Magnitude: FixedWidthInteger & UnsignedInteger. This is really just making the existing semantics explicit, and resolves the immediate issue entirely. We may also want to make next( ) work with SignedInteger, but we can do that as a separate bug / enhancement, as it's not necessary to resolve the immediate ergonomics issue. |
Additional Detail from JIRA
md5: cd2383b8df0c4be4e2e4a03e4f022a94
Issue Description:
One would like to be able to write code like the following:
What's actually necessary here is:
But that's a lot to ask of people.
The text was updated successfully, but these errors were encountered: