Enforce `@Observable` through a protocol

I came across the same need. And I figured that I could simply have an Observable class always the same to carry values, and make it get-only in the protocol. It’s a bit disappointing since it makes you shape your API differently. You really just use @Observable as part of it and your protocol brings the abstraction for the methods.

What do you think?

@Observable
public class InjectedDependencyImplementingValues {
    public var someOBservableValue: Int
    public init(someOBservableValue: Int) {
        self.someOBservableValue = someOBservableValue
    }
}

public protocol InjectedDependencyImplementing {
    var values: InjectedDependencyImplementingValues { get }

    func doSomething()
}
1 Like