Skip to content

Swift macro that proxies a value from one object to another.

Notifications You must be signed in to change notification settings

simonbs/ProxyMacro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ProxyMacro

Swift macro that proxies a property from one object to another.

final class MyObj {
    private final class StateStore {
      var number = 42
    }

    @Proxy(\Self.stateStore.number)
    var number: Int

    private let stateStore = StateStore()
}

This is useful when state needs to be kept in sync between multiple objects but we do not want to forward the state using willSet/didSet. In this case we pass the state store to the children instead. We can still expose the number property on MyObj and any reads and writes will be forwarded to the number propety on StateStore.

final class MyObj {
    private final class StateStore {
      var number = 42
    }

    @Proxy(\Self.stateStore.number)
    var number: Int

    private let stateStore = StateStore()
    private lazy var childA = Child(stateStore: stateStore)
    private lazy var childB = Child(stateStore: stateStore)
}

final class Child {
    private let stateStore: StateStore

    init(stateStore: StateStore) {
        self.stateStore = stateStore
    }
}

About

Swift macro that proxies a value from one object to another.

Topics

Resources

Stars

Watchers

Forks

Languages