示例工程 07-optionals

07-optionals/Sources/Ch07Demo/App.swift

swift · 2.0 KB

import Ch07Kit
 
@main
struct Ch07Demo {
    static func main() {
        let withAddress = User(name: "ada", address: Address(city: "London"))
        let withoutAddress = User(name: "bob", address: nil)
        print(
            "[7.1] city = \(OptionalBasics.city(of: withAddress))/\(OptionalBasics.city(of: withoutAddress))"
        )
        print("[7.1] greeting = \(OptionalBasics.greeting(for: withAddress))")
        print(
            "[7.1] normalized = \(OptionalBasics.normalized("  ") ?? "nil")/\(OptionalBasics.normalized("ADA") ?? "nil")"
        )
        print(
            "[7.1] describe = \(OptionalBasics.describe(nil))/\(OptionalBasics.describe(0))/\(OptionalBasics.describe(7))"
        )
 
        print("[7.2] increment = \(OptionalTransforms.increment(1).map(String.init) ?? "nil")")
        print("[7.2] nestedParse = \(String(describing: OptionalTransforms.nestedParse("12")))")
        print(
            "[7.2] flattenedParse = \(OptionalTransforms.flattenedParse("12").map(String.init) ?? "nil")"
        )
        print("[7.2] compactMap = \(OptionalTransforms.parseAll(["1", "x", "3"]))")
        print("[7.2] rawParse = \(OptionalTransforms.rawParse(["1", "x", "3"]))")
 
        print("[7.3] try? = \(ErrorLoss.withTryQuestion("x").map(String.init) ?? "nil")")
        print(
            "[7.3] reason = \(ErrorLoss.reason(""))/\(ErrorLoss.reason("x"))/\(ErrorLoss.reason("3"))"
        )
 
        print(
            "[7.4] nested = \(NestedOptional.describe(NestedOptional.someNil))/\(NestedOptional.describe(NestedOptional.none))"
        )
        print(
            "[7.4] flatten = \(NestedOptional.flattenOneLevel(NestedOptional.someNil).map(String.init) ?? "nil")"
        )
        print("[7.4] dictionary = \(NestedOptional.dictionaryLookup())")
 
        print("[7.5] MemoryLayout<Int?> = \(MemoryLayout<Int?>.size) 字节")
        print("[7.5] MemoryLayout<Bool?> = \(MemoryLayout<Bool?>.size) 字节")
        print("[7.5] MemoryLayout<User> = \(MemoryLayout<User>.size) 字节")
    }
}