Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Generalized accessors

Generalized accessors

Takuma Shimizu

March 27, 2019
Tweet

More Decks by Takuma Shimizu

Other Decks in Programming

Transcript

  1. subscrit
    get set

    View full-size slide

  2. get
    let nums = Array(0..<10)
    let x10 = nums[3] * 10 // `get`
    print(num)
    subscript(index: Int) -> Element {
    get {
    let element = // `index`
    return element //
    }
    set { ... }
    }

    View full-size slide

  3. set
    var nums = Array(0..<10)
    let num = nums[3]
    nums[3] = num * 10 // `set`
    print(num)
    subscript(index: Int) -> Element {
    get { ... }
    set(newValue) { // `newValue`
    // buffer
    = newValue
    }
    }

    View full-size slide

  4. Swift 4.2
    mutableAddressWithPinnedNativeOwner
    Swift 5
    read
    ,
    modify
    yield
    co-routine

    View full-size slide

  5. read
    // CoreAudio.swift
    subscript(index: Index) -> Element {
    _read {
    yield ((_audioBuffersPointer + index).pointee)
    }
    }
    modify
    // Array.swift
    subscript(index: Int) -> Element {
    _modify {
    let address = _buffer.subscriptBaseAddress + index
    yield &address.pointee
    }
    }

    View full-size slide

  6. yield
    subscript(index: Int) -> inout Element {}
    inout
    Rust Lifetime
    Rust
    Swift

    View full-size slide