[swift-evolution] [Proposal] Random Unification

Mike Kluev mike.kluev at gmail.com
Wed Oct 11 15:39:17 CDT 2017


On 11 October 2017 at 18:30, Mike Kluev <mike.kluev at gmail.com> wrote:

> > On 7 Oct 2017, at 04:24, Chris Lattner via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>> >
>> >> 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.
>>
>
> or make it a non-initialiser: let x = Int.random or Int.random()
>

on the positive side would be ability to omit types in many cases:

foo(_ x: Int) { ... }

foo(.random)   // Int.random inferred


> have you guys considered: func random<T>(...) -> T
> where it returns a different type based on a context?
>

or "foo(random())" here,  random<Int>() inferred

the latter case opens wide opportunities for strict type/range checking,
e.g.:

if "angle" is a variable ranging from 0 .. 2*pi then

angle = random()   // is inferred as a 0 .. 2*pi range without me typing
anything

Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171011/c8785c11/attachment.html>


More information about the swift-evolution mailing list