extension of nested types

Swift โŸฉ Types โŸฉ Nested Types โŸฉ extension of nested types

ๅ•๏ผšใ€Œๅฆ‚ไฝ•ๅฎฃๅ‘Š generic type ็š„ nested type ็š„ extension ๅ‘ข๏ผŸใ€

// โญ๏ธ generic type: A<T>
struct A<T> {
    let item: T
}

// โญ๏ธ nested type: A.B
extension A {
    struct B {
        let foo: Bool
    }
}

// โญ๏ธ extension of nested type
//    ๆณจๆ„๏ผš้€™่ฃกไธ่ƒฝๅฏซๆˆ extension `A<T>.B`
extension A.B {
    var notFoo: Bool { return !foo }
}

// test
let a = A(item: "Hi")
let b = A<String>.B(foo: true)

print(b.notFoo)  // false

Last updated