I have a protocol
protocol doSomethingProtocol {
associatedtype someType
}
then i have class that is implementing this protocol
class doSomethingClass : doSomethingProtocol {
typealias someType = Int
}
Now i want to use this protocol as a reference in some other class
class someClass : ObservableObject {
private var reference : doSomethingProtocol
}
Now i cannot do this as doSomethingProtocol
has an associatedtype. So i decide to use some
class someClass : ObservableObject {
private var reference : some doSomethingProtocol
init(){
reference = doSomethingClass()
}
}
However this doesn't work. I get the error Property declares an opaque return type, but has no initializer expression from which to infer an underlying type
. Why ? I am giving it initializer expression in the class init.
However when i do something like this
class someClass : ObservableObject {
private var reference : some doSomethingProtocol = doSomethingClass()
init(){}
}
I get no error messages and it compiles. Why, what is the difference between the two.
doSomethingProtocol
w/o associated type specialization. – Hern