示例工程 12-opaque-existential-types

12-opaque-existential-types/Diagnostics/MismatchedOpaque.swift

swift · 0.4 KB

// 故意编译失败:同一个不透明返回类型不能有两个底层类型。
// 复现:swiftc Diagnostics/MismatchedOpaque.swift
protocol Shape {
    func area() -> Double
}
 
struct Circle: Shape {
    func area() -> Double { 3 }
}
 
struct Square: Shape {
    func area() -> Double { 4 }
}
 
func make(_ flag: Bool) -> some Shape {
    if flag {
        return Circle()
    } else {
        return Square()
    }
}
 
print(make(true).area())