Something about protocol of swift
protocol GrandProtocol {
associatedtype ExpectType : Codable
func printt(codable : ExpectType) -> ExpectType
}
class TypeA : GrandProtocol{
typealias TypeAExpectType = String//just confirm codable
func printt(codable: TypeAExpectType) -> TypeAExpectType {
return "value to be return"
}
}
class TypeZ : Decodable , Encodable {}
protocol SubProtocolCertainExpectType : GrandProtocol where ExpectType == TypeZ {}
class TypeC : SubProtocolCertainExpectType{
typealias TypeCExpectType = TypeZ //must be TypeZ
func printt(codable : TypeCExpectType) -> TypeCExpectType{
return TypeZ()
}
}
protocol SubProtocolWhichExpectTypeIskindOfTypeZWhichConfirmCodable : GrandProtocol where ExpectType : TypeZ {}
class SubTypeZ: TypeZ {
}
class TypeD : SubProtocolWhichExpectTypeIskindOfTypeZWhichConfirmCodable{
typealias TypeDExpectType = SubTypeZ//should be TypeZWhichConfirmCodable or sub class of TypeZWhichConfirmCodable
func printt(codable : TypeDExpectType) -> TypeDExpectType{
return SubTypeZ()
}
}
protocol RestrainPropertyType {
associatedtype ExceptType : Codable
}
class SomeType: RestrainPropertyType {
typealias ExceptType = String
var property : ExceptType?
}
func usage() {
let resultOf = SomeType().property?.count
print(resultOf)
}