I'm not really convinced. From the proposition here , my code should be translated to :
@ChildNode(withName: "obj")
private var myObj: MyProtocol = MyClass()
// translats to :
private var _myObj: ChildNode<MyProtocol> = ChildNode<MyProtocol>(withName: "obj", wrappedValue: MyClass())
var myObj: MyProtocol {
get { return _myObj.wrappedValue }
set { _myObj.wrappedValue = newValue }
}
And I don't know any restriction on using generic with a protocol like that.
For instance, with this file (used in my exemple above)
import SpriteKit
public protocol MyProtocol:SKNode
{
var myProperty:String{get set}
}
public class MyClass:SKNode, MyProtocol
{
public var myProperty: String = "my property!"
}
public class MyGenericClass<T>
{
public var myObj:T
init(myObj:T)
{
self.myObj = myObj
}
}
And this usage :
let t = MyGenericClass<MyProtocol>(myObj: MyClass())
print(t.myObj.myProperty)
The code compiles and works just fine and the property is printed.
Also, from my experience the whole point of having protocol is polymorphism and having a mandatory abstract implementation of it makes it a bit cumbersome.
Also, after further investigation, I realise that the problem occurs when we add a type requirement on the generic.
Like that :
public class MyGenericClass<T:SKnode>
{
public var myObj:T
init(myObj:T)
{
self.myObj = myObj
}
}
Now, for what i understand, because MyProtocol is a protocole and SKNode is a class, the requirement on the protocole "public protocol MyProtocol:SKNode" doesn't actually says that MyProtocol is a SKNode hence the compilation error.
The question is : is it normal?
PS: I will close this post and open a new one regarding the generic and protocol observation.