<div dir="ltr">Hi All,<div><br></div><div>Anyone know what is going on here:</div><div><br></div><div> //: Closure picks up static type not dynamic</div><div> <br> class MutableReference<T> {<br> init() {<br> guard type(of: self) != MutableReference.self else {<br> fatalError("MutableReference is an abstract class; create a derrivative of MutableReference")<br> }<br> }<br> var value: T {<br> get {<br> fatalError("Calculated property value getter should be overridden")<br> }<br> set {<br> fatalError("Calculated property value setter should be overridden")<br> }<br> }<br> }</div><div> <br> class DefaultMutableReference<T>: MutableReference<T> {<br> private var _value: T<br> override var value: T {<br> get {<br> return _value<br> }<br> set {<br> _value = newValue<br> }<br> }<br> init(_ value: T) {<br> _value = value<br> }<br> }<br><br> let e: (MutableReference<[Int]>, Int) -> Void = { $0.value.append($1) }<br> let dmr = DefaultMutableReference([2])<br> dmr.value // [2]<br> e(dmr, 2) // fatal error: Calculated property value getter should be overridden<br> dmr.value // Expect [2, 2] <br><p class="gmail-p1">If I change `e` to:</p> let e: (DefaultMutableReference<[Int]>, Int) -> Void = { $0.value.append($1) }<br><p class="gmail-p1">It works fine.</p><p class="gmail-p1">IE the closure is using the static type of its first argument and not dynamically dispatching.</p><p class="gmail-p1">Am I doing something wrong? Is there a way round where I can still use the base class for `e`?</p><p class="gmail-p1"><br></p><p class="gmail-p1">Thanks for any help in advance,</p><p class="gmail-p1">-- Howard.<br></p>
</div></div>