[swift-evolution] [Proposal] Random Unification

Chris Lattner clattner at nondot.org
Fri Oct 6 22:24:53 CDT 2017


> On Oct 5, 2017, at 10:58 AM, Nate Cook via swift-evolution <swift-evolution at swift.org> wrote:
> 
> The edge case is really the same (empty ranges), it’s about what we do with the edge case. If we include the methods on integer types, usage will look like this:
> 
>     let x = Int.random(in: 0..<5)     // 3
>     let y = Int.random(in: 0..<0)     // runtime error
> 
> If we only have the collection methods, usage will look like this:
> 
>     let x = (0..<5).random()!         // 3
>     let y = (0..<0).random()!         // runtime error

These aren’t the forms I was suggesting, what I meant was:

extension Int {
  init(randomInRange: Countable{Closed}Range<Int>)
}

which gives:
	let x = Int(randomInRange: 0..<5)

The point of this is that you’re producing an Int (or whatever type).  Regardless of whether the initializer is failable or not, this is the preferred way of creating a new value with some property: it is an initializer with a label.

-Chris


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171006/28c2c4f0/attachment.html>


More information about the swift-evolution mailing list