Is it possible to implement a protocol type var with a concrete class which implements this protocol?
Does that sound confusing? Here is an example :-)
// A protocol requires some var. And an implementation of this class
protocol SomethingProtocol {
var something: String { get }
}
class SomethingConcrete: SomethingProtocol {
var something: String { "something" }
var someOther: String { "something else" }
}
// A protocol which requires someValue of protocol type SomethingProtocol
protocol EntityProtocol {
var someValue: SomethingProtocol { get }
}
// Implementation of EntityProtocol
class ConcreteEntiy: EntityProtocol {
// DOES NOT WORK:
// Implementing the var someValue of required typ SomethingProtocol
// using its implementation SomethingConcret
//
// Error: Type 'ConcretEntiy' does not conform to protocol 'EntityProtocol'
var someValue: SomethingConcrete = SomethingConcrete()
// DOES WORK:
// Implementation as SomethingProtocol as required by EntityProtocol
// but no access to others values of SomethingConcret
var someValue: SomethingProtocol = SomethingConcrete()
// Error: Value of type 'SomethingProtocol' has no member 'somethingElse'
let otherValue = someValue.somethingElse
}
EntityProtocolrequires avar someValueof protocol typeSomethingProtocolSomethingConcreteimplementsSomethingProtocoland thus is of typeSomethingProtocolConcreteClassimplementsEntityProtocoland thus needs avar someValueof protocol typeSomethingProtocol
Why is it not possible to full fill the requirement of EntityProtocol by providing a var someValue of type SomethingConcrete?