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

David Sweeris davesweeris at mac.com
Wed Dec 28 19:07:48 CST 2016


> On Dec 28, 2016, at 14:24, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
>> On Wed, Dec 28, 2016 at 5:21 PM, Anton Zhilin <antonyzhilin at gmail.com> wrote:
>> What about “Rust way”?
>> 
>> protocol Default {
>>     static var `default`: Self { get }
>> }
>> 
>> protocol Setupable {
>>     mutable func setup(with params: Params)
>> }
>> 
>> func setupArray<T>(type: T.Type, params: Params) -> [T]
>>     where T: Default & Setupable
>> {
>>     return (1...42).map { i in
>>         var next = T.default
>>         next.setup(with: params)
>>         return next
>>     }
>> }
>> default is literally a “default” value. For primitives like Int, Char, Bool, Float etc. it is all zeros. For structs with Default fields, it’s all defaults—unless some special semantics is given to them. For complex types, it’s an “empty” value, the most sane value before any setup or specification.
>> 
> 
> The objection is not about the spelling. The point here is that such a protocol does not make sufficient semantic guarantees to enable any interesting generic algorithms not possible without it. As I pointed out, Rust explicitly makes no guarantees as to what the default value is and tells you that you must not care; "most sane" does not cut it as a guarantee.

Correct. Treating protocols as "bags of syntax" is tempting (I still find myself accidentally doing it from time to time), but that's not what a Swift protocol is.

Perhaps adding syntax to express a "bag of syntax" could be discussed either as its own topic (probably phase 2, but that's not my call), or later on as part of the macro system (it does sound rather macroish, IMHO).

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161228/3805edf4/attachment.html>


More information about the swift-evolution mailing list