[swift-users] Static type when expecting dynamic type

Jordan Rose jordan_rose at apple.com
Thu Feb 2 11:04:19 CST 2017


Seems like a bug, and moreover it seems like a regression from Swift 3.0. Mind filing a report at bugs.swift.org <http://bugs.swift.org/>?

Thanks!
Jordan

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170202/f84670f2/attachment.html>


More information about the swift-users mailing list