示例工程 12-opaque-existential-types

12-opaque-existential-types/Sources/Ch12Demo/App.swift

swift · 1.1 KB

import Ch12Kit
import Foundation
 
@main
struct Ch12Demo {
    static func main() {
        let circle = OpaqueLab.unitCircle()
        print("[12.1] some 返回 = \(circle.name),面积 = \(String(format: "%.3f", circle.area()))")
        print("[12.1] 泛型参数 = \(OpaqueLab.doubledArea(of: circle))")
 
        let square = OpaqueLab.unitSquare()
        print("[12.1] 另一个不透明类型 = \(square.name)")
 
        let shapes = ExistentialLab.shapes()
        print("[12.2] any 数组 = \(shapes.map(\.name).sorted())")
        print("[12.2] 总面积 = \(String(format: "%.3f", ExistentialLab.totalArea(shapes)))")
 
        print("[12.3] any Collection<Int> = \(ExistentialLab.countInts([1, 2, 3]))")
        print("[12.3] Set 也满足 = \(ExistentialLab.countInts(Set([1, 2, 3])))")
 
        print("[12.4] MemoryLayout<any Shape> = \(MemoryLayout<any Shape>.size) 字节")
        print("[12.4] MemoryLayout<Circle> = \(MemoryLayout<Circle>.size) 字节")
        print("[12.4] 具体类型 = \(String(describing: type(of: circle)))")
        print("[12.4] 存在类型 = \(String(describing: type(of: shapes[0] as Any)))")
    }
}