[swift-evolution] [Proposal] Random Unification

Ben Cohen ben_cohen at apple.com
Thu Oct 5 11:30:23 CDT 2017



> On Oct 4, 2017, at 9:12 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> ```
>> extension Int {
>>   static func random(in range: Countable{Closed}Range<Int>) -> Int
>> }
> 
> Nice.  Should these be initializers like:
> 
> extension Int {
>   init(randomIn: Countable{Closed}Range<Int>)
> }
> 

I don’t see much of a case for making it it random(in: SpecificCollection) instead of genericCollection.random().

One possible reason is if you exclude half-open ranges, only having CountableClosedRange, then you don’t have to account for the possibility of an empty collection (via an optional or a trap) because they cannot be empty. But closed ranges aren’t the currency type – half-open ranges are. So it’d hit usability if you have to convert from one to t'other often.

Other possibility is discovery. But given the common use case is “random element from collection”, I don’t expect this to be an issue as it will quickly become common knowledge that this feature is available.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171005/42c179e8/attachment.html>


More information about the swift-evolution mailing list