Skip to content

[SR-8156] Difficult to use random integers in generic contexts #50688

@stephentyrone

Description

@stephentyrone
Previous ID SR-8156
Radar rdar://problem/41789616
Original Reporter @stephentyrone
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee @stephentyrone
Priority Medium

md5: cd2383b8df0c4be4e2e4a03e4f022a94

Issue Description:

One would like to be able to write code like the following:

(swift) func foo<T>( ) -> T where T: FixedWidthInteger {
          return T.random(in: 1 ... 6)
        }
<REPL Input>:2:10: error: ambiguous reference to member 'random(in:using:)'
  return T.random(in: 1 ... 6)
         ^
Swift.FixedWidthInteger:2:35: note: found this candidate
    @inlinable public static func random<T>(in range: Range<Self>, using generator: inout T) -> Self where T : RandomNumberGenerator
                                  ^
Swift.FixedWidthInteger:3:35: note: found this candidate
    @inlinable public static func random(in range: Range<Self>) -> Self
                                  ^
Swift.FixedWidthInteger:2:35: note: found this candidate
    @inlinable public static func random<T>(in range: ClosedRange<Self>, using generator: inout T) -> Self where T : RandomNumberGenerator
                                  ^
Swift.FixedWidthInteger:3:35: note: found this candidate
    @inlinable public static func random(in range: ClosedRange<Self>) -> Self

What's actually necessary here is:

(swift) func foo<T>( ) -> T where T: FixedWidthInteger, T.Stride: SignedInteger, T.Magnitude: UnsignedInteger {
          return T.random(in: 1 ... 6)
        }

But that's a lot to ask of people.

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.standard libraryArea: Standard library umbrella

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions