What about failable literal convertibles?


(David Sweeris) #1

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