5

Can anyone explain to me what this error means and what is wrong with this attempted implementation of a Weak wrapper in Swift.

Just copy this into a new Playground:

import Cocoa

public struct Weak<T> where T: AnyObject {
    weak var value: T?
    
    init (_ value: T?) {
        self.value = value
    }
}

public protocol MyProto: class {
}

public class Foo: MyProto {
}

var foos = [Weak<MyProto>]()
let foo = Foo()

for foo in foos {
    if let bar = foo.value {  // 'MyProto' is not convertible to 'AnyObject'
    }
}

foos.append(Weak(foo as MyProto))  // Cannot convert value of type 'MyProto' to expected argument type '_?'

I get the errors:

error: 'MyProto' is not convertible to 'AnyObject'

error: cannot convert value of type 'MyProto' to expected argument type '_?'

Community
  • 1
  • 1
devios1
  • 36,899
  • 45
  • 162
  • 260
  • 3
    See the comments on https://stackoverflow.com/q/47440451/2976878 – Hamish Nov 29 '17 at 22:06
  • Does this mean I have to give up on generics and explicitly cast everything? Ugh. – devios1 Nov 29 '17 at 22:10
  • Why are you using `AnyObject` instead of `Any`? – creeperspeak Nov 29 '17 at 22:16
  • @devios1 Is marking the protocol `@objc` a potential solution for you? `@objc` protocols can conform to themselves. Otherwise you'll want a type eraser. – Hamish Nov 29 '17 at 22:21
  • 1
    @creeperspeak weak' may only be applied to class and class-bound protocol types – Will M. Nov 29 '17 at 22:23
  • All I'm trying to do is hold an array of weak references to protocols. Is there a saner way? – devios1 Nov 29 '17 at 22:28
  • @Hamish Well I can try converting all my classes to `@objc`, if that will solve it. But I've already been down that path and don't really want to go back. :/ – devios1 Nov 29 '17 at 22:31
  • @devios1 The classes don't have to be explicitly `@objc` (and therefore don't have to inherit from `NSObject`) in order to conform to an `@objc` protocol. – Hamish Nov 29 '17 at 22:37
  • @Hamish Unfortunately the methods I actually need in the protocol need to reference my main `Node` class and can't represent it in the protocol unless it is also `@objc`, creating a chain reaction of dependencies. – devios1 Nov 29 '17 at 22:39
  • I'm researching Type Erasers now… – devios1 Nov 29 '17 at 22:49
  • I couldn't figure out any way to do a type eraser for a weak reference so I ended up just forgetting about generics and implementing a concrete weak class for each place it is needed. Really really sad that I am forced to do this when generics could have been such a reusable solution. – devios1 Nov 29 '17 at 23:03

0 Answers0