on Fri Jun 23 05:26:11 CDT 2017 Haravikk swift-evolution at haravikk.me wrote:
> Not sure what you mean by added indirection here, the following seems perfectly straightforward to me: > > protocol Foo { > var someValue:Int { get set } > func a() -> Any? > } > > extension Foo { > func a() -> Any? { return self.someValue } > } > > struct ValueSemantics:Foo { var someValue:Int } > class ReferenceSemantics:Foo { > var someValue:Int { return nil } > } > > There is no added access overhead here, the only difference is that the protocol itself leaves it up to > implementations whether someValue is stored or computed. in real cases there would be more variables: //============ protocol P1 { // #noise var var1: Int { get set } // #noise var var2: Int { get set } // #noise // ... // #noise x 100 var var100: Int { get set } // #noise func foo1() -> Int // #noise func foo2() -> Int // #noise // ... // #noise x 100 func foo100() -> Int // #noise } extension P1 { // #noise func foo1() -> Int { return var1 * 2 } func foo2() -> Int { return var2 * 2 } // ... func foo100() -> Int { return var100 * 2 } } struct S1: P1 { var var1: Int // #noise var var2: Int // #noise // ... // #noise x 100 var var100: Int // #noise } class C1: P1 { var var1: Int = 0 // #noise var var2: Int = 0 // #noise // ... // #noise x 100 var var100: Int = 0 // #noise } //============ lots of noise and violations of DRY. you may try to mitigate it by putting all those storage into another struct, that was the indirection i was thinking about: //============ struct Pimpl { // #noise var var1: Int = 0 var var2: Int = 0 // ... var var100: Int = 0 func foo1() -> Int { return var1 * 2 } func foo2() -> Int { return var2 * 2 } // ... func foo100() -> Int { return var100 * 2 } } protocol P2 { // #noise var pimpl: Pimpl { get set } // #noise func foo1() -> Int // #noise func foo2() -> Int // #noise // ... // #noise x 100 func foo100() -> Int // #noise } extension P2 { // #noise func foo1() -> Int { return pimpl.var1 * 2 } // #indirection func foo2() -> Int { return pimpl.var2 * 2 } // #indirection // ... // #indirection x 100 func foo100() -> Int { return pimpl.var100 * 2 } // #indirection } struct S2: P2 { var pimpl: Pimpl // #noise init() { pimpl = Pimpl() // #noise } } class C2: P2 { var pimpl: Pimpl // #noise init() { pimpl = Pimpl() // #noise } } //============ while the proposed solution has minimal amount of noise: //============ struct S3 { var var1: Int var var2: Int // ... var var100: Int func foo1() -> Int { return var1 * 2 } func foo2() -> Int { return var2 * 2 } // ... func foo100() -> Int { return var100 * 2 } } class C3: S3 { } // =========== Mike
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution