[swift-evolution] [Pitch] Add the DefaultConstructible protocol to the standard library

Braeden Profile jhaezhyr12 at gmail.com
Mon Dec 26 23:10:06 CST 2016


I forgot to reply to the email list here.

> On Dec 26, 2016, at 10:03 PM, Xiaodi Wu <xiaodi.wu at gmail.com <mailto:xiaodi.wu at gmail.com>> wrote:
> 
> Did you mean to reply offlist?
> 
> Basically, a factory would be:
> 
> func make<T>(_: T.Type) -> T { return T() }
> 
> but you need T to conform to a protocol that guarantees T.init(). You use it like:
> 
> make(Foo.self)
> 
> But one can write:
> 
> func make<T>(_ f: @autoclosure () -> T) -> T { return f() }
> 
> and use this like:
> 
> make(Foo())
> 
> (pardon typos; freehanding on iPad)
> 
> 
> 
> On Mon, Dec 26, 2016 at 23:58 Braeden Profile <jhaezhyr12 at gmail.com <mailto:jhaezhyr12 at gmail.com>> wrote:
> Actually, I’m a little lost at this point of exactly how you could build a function with this pattern.  Code, anyone?
> 
>> On Dec 26, 2016, at 9:57 PM, Xiaodi Wu <xiaodi.wu at gmail.com <mailto:xiaodi.wu at gmail.com>> wrote:
>> 
>> With @autoclosure your user can simply write T() as an argument. It is very elegant and not complicated at all.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161226/3aca3643/attachment.html>


More information about the swift-evolution mailing list