[swift-evolution] TreeLiteralConvertible
John McCall
rjmccall at apple.com
Thu Apr 14 16:24:21 CDT 2016
> On Apr 14, 2016, at 2:20 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
>> No, you just need Tree to conform to both ArrayLiteralConvertible and IntegerLiteralConvertible, and it implements the latter by building a Value out of it.
>
> That not only doesn't work if your type isn't a LiteralConvertible, it also doesn't work if you want to build a literal with variables:
>
> let myTree: Tree = [1, [2, three]]
>
> The real missing feature here is implicit lifting like Optional offers. With a LiftingConvertible protocol, you could say something like this:
>
> enum Tree<Value> {
> case leaf(Value)
> case branches([Tree<Value>])
> }
>
> extension Tree: ArrayLiteralConvertible {
> init(arrayLiteral branches: Tree<Value>...) {
> self = .branches(branches)
> }
> }
>
> extension Tree: LiftingConvertible {
> init(lifting value: Value) {
> self = .leaf(value)
> }
> }
Another name for this feature is "user-defined implicit conversions".
John.
More information about the swift-evolution
mailing list