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

Anton Zhilin antonyzhilin at gmail.com
Wed Dec 28 16:21:25 CST 2016


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.
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161229/5d756145/attachment.html>


More information about the swift-evolution mailing list