why is this causing so much trouble? (protocols and typealiases on their associated types)

后端 未结 2 1282
臣服心动
臣服心动 2021-01-21 17:00

I\'m trying to do something along the lines of this:

protocol Protocol {
    associatedtype T
    associatedtype ArrayT = Array
}

struct Struct

        
2条回答
  •  一个人的身影
    2021-01-21 17:25

    The line associatedtype ArrayT = Array only tells the compiler that the default value of ArrayT is Array. An adaption of the protocol can still change ArrayT like:

    struct U: Protocol {
        typealias T = UInt32
        typealias ArrayT = UInt64   // <-- valid!
    }
    

    If you want a fixed type, you should use a typealias...

    // does not work yet.
    protocol Protocol {
        associatedtype T
        typealias ArrayT = Array
    }
    

    But the compiler complains that the type is too complex

提交回复
热议问题