[swift-evolution] What about failable literal convertibles?

David Sweeris davesweeris at mac.com
Tue Apr 12 23:46:32 CDT 2016


What about having a protocol for having failable literal convertible initializers for literal optionals?
struct AtLeast3<T> : FailableArrayLiteralConvertible {
    ...
    init?(arrayLiteral elements: T...) {
        guard elements.count >= 3 else { return nil }
        ...
    }
}
var bar: AtLeast3? = [1, 2]? // bar is 'Optional<AtLeast3>.None'
bar = [1,2,3] // bar is 'Optional<AtLeast3>.Some([1,2,3])'

Seems handy for when your types are literal convertible in principle, but you need to do some validation.

Thoughts?

- Dave Sweeris


More information about the swift-evolution mailing list