Use case: SwiftUI AdaptiveView Protocol: Default vs Custom Implementation Issue
I can reduce that to the following. Can it be done without the extra protocol?
protocol UselessProtocol {
associatedtype Input
static var input: Input { get }
}
protocol Derived: UselessProtocol {
associatedtype Output
static func method(_: Input) -> Output
}
extension Derived {
static var input: some Any { "default" }
static var property: Output { method(input) }
static func method(_: Input) -> some Any { input }
}
protocol SingleProtocol {
associatedtype Input
associatedtype Output
static var input: Input { get }
static func method(_: Input) -> Output
}
extension SingleProtocol {
static var input: some Any { "default" }
static var property: Output { method(input) }
static func method(_: Input) -> some Any { input }
}
enum DerivedDefault: Derived { }
enum DerivedOverrider: Derived {
static func method(_: Input) -> some Any { "override" }
}
enum SingleProtocolDefault: SingleProtocol { }
enum SingleProtocolOverrider: SingleProtocol {
static func method(_: Input) -> some Any { "override" }
}
func cast(_ property: some Any) -> String { property as! String }
#expect(cast(DerivedDefault.property) == "default") // ✅
#expect(cast(DerivedOverrider.property) == "override") // ✅
#expect(cast(SingleProtocolDefault.property) == "default") // ✅
#expect(cast(SingleProtocolOverrider.property) == "override") // ❌ It's still "default".