var defined in protocol doesn't conform to multiple protocol - swift

I'm struggling with protocols in Swift.
I have defined a protocol like this:
protocol AProtocol {
var property : BProtocol {get set}
}
And I would like to conform to AProtocol in a class with a property that also conform to another protocol. I've tried in these two ways:
class AClass: AProtocol {
var property = BClass()
}
and:
class AClass: AProtocol {
var property: BProtocol & MyBClassType = BProtocol()
}
but none of them seems to work (BClass itself confirm to BProtocol)
This issue is a bit difficult to explain, I hope it was clear.
Is it a limitation of the Swift language?
Do you know a work around to this?

You have two issues: firstly, the property name must match that declared in the protocol, secondly you need to type annotate the variable to be of type BProtocol as Hamish explained in the comment.
protocol AProtocol {
var aProperty : BProtocol {get set}
}
protocol BProtocol {}
class BClass: BProtocol {}
class AClass: AProtocol {
var aProperty: BProtocol = BClass()
}
You should also conform to the Swift naming convention, which is lowerCamelCase for variable names, so I changed AProperty to its correct form, aProperty.

Related

How can I use a generic-type constraint extension for a class with a `Codable` type constraint?

Say my class definition is:
class Foo<T: Codable> {
let bar: T
}
I want to extend arrays of this type:
extension Array where Element: Foo<Codable> { /* do something based on `bar` values */ }
This produces the error
Protocol 'Codable' (aka 'Decodable & Encodable') as a type cannot conform to 'Decodable'
I did read in another question that conforming T to Encodable/Decodable isn't possible if T == Encodable/Decodable since a protocol can't conform to itself, but this is only for a certain property so I'm having trouble wrapping my head around it. Is there any way to achieve this?
I think is better to use protocol instead class like this.
protocol FooProtocol {
associatedtype T : Codable
var bar: T {get set}
}
extension Array where Element: FooProtocol {
}

Swift Protocol: property that is subclass of: someClass, instead of being of that class

I'm trying to create a protocol that describes a property that is a subclass of a certain class instead of being of that class.
I would like to know if something like this is possible.
protocol {
var prop : T where T: SomeClass { get set} // something like this
}
versus
protocol {
var prop : SomeClass {get set}
}
Use associatedtype in your protocol definition
protocol pp {
associatedtype T where T: SomeClass
var prop : T { get set}
}

How do you structure generic type protocol conformance in Swift?

The following contrived Swift 2 example from real-world code won't compile:
protocol SomeModelType { }
protocol SomeProtocol {
var someVar: SomeModelType? { get }
}
class ConcreteClass<T: SomeModelType>: SomeProtocol {
var someVar: T?
}
This doesn't make sense to me fully. I would assume in ConcreteClass that because I have T being constrained to SomeModelType and have T as the backing type for the someVar property, the compiler would be able to figure out that the SomeProtocol was being conformed to by ConcreteClass.
How should an example like this be structured? Is it possible to the Swift compiler to determine protocol conformance through generic type constraints?
protocol SomeModelType { }
protocol SomeProtocol {
associatedtype T: Any
var someVar: T? { get }
}
class ConcreteClass<T> :SomeProtocol where T: SomeModelType {
var someVar: T?
}

Generic variables in protocols with associated types. Bug? [duplicate]

This question already has answers here:
Unable to use protocol as associatedtype in another protocol in Swift
(2 answers)
Closed 6 years ago.
I'm using Xcode 7.3 and Swift 2.3. I have difficulties using protocols with associated types that have variables. Look at the example:
protocol SomeProtocol {}
class SomeProtocolImpl: SomeProtocol {}
protocol ProtocolWithAssociatedType {
associatedtype T: SomeProtocol
var variable: T { get }
}
class TestClass: ProtocolWithAssociatedType {
var variable: SomeProtocol = SomeProtocolImpl()
}
For some reason, compiler shows errors:
How is that possible? Am I doing something wrong? Is it a bug? A known one?
What I've tried:
defined typealias for that associated type:
class TestClass: ProtocolWithAssociatedType {
typealias O = SomeProtocol
var variable: SomeProtocol = SomeProtocolImpl()
}
nope.
Used method instead:
protocol SomeProtocol {}
class SomeProtocolImpl: SomeProtocol {}
protocol ProtocolWithAssociatedType {
associatedtype T: SomeProtocol
func someMethod() -> T
}
class TestClass: ProtocolWithAssociatedType {
typealias T = SomeProtocol
func someMethod() -> SomeProtocol {
return SomeProtocolImpl()
}
}
just got different errors:
How should I create protocol with associated type and variable and avoid this errors?
The compiler needs a specific type for T that conforms to SomeProtocol.
While this line looks correct
var variable: SomeProtocol
the problem the compiler is running into is that SomeProtocol is not a type, it's a protocol.
The easiest solution, tell the compiler a type to use
var variable: SomeProtocolImpl = SomeProtocolImpl()
or just let it figure it out itself.
var variable = SomeProtocolImpl()

protocol associated type typealias assignment compile error

Following code:
protocol SomeProtocol {
typealias SomeType = Int // used typealias-assignment
func someFunc(someVar: SomeType)
}
class SomeClass: SomeProtocol {
func someFunc(someVar: SomeType) {
print(someVar)
}
}
gives compile-time error:
Use of undeclared type 'SomeType'
Adding, say typealias SomeType = Double, to the SomeClass resolves the error.
The question is, what's the point of typealias-assignment part (which is optional btw) of protocol associated type declaration though?
In this case the assignment of Int to the typealias is equal to no assignment because it gets overridden by your conforming type:
// this declaration is equal since you HAVE TO provide the type for SomeType
protocol SomeProtocol {
typealias SomeType
func someFunc(someVar: SomeType)
}
Such an assignment provides a default type for SomeType which gets overridden by your implementation in SomeClass, but it is especially useful for protocol extensions:
protocol Returnable {
typealias T = Int // T is by default of type Int
func returnValue(value: T) -> T
}
extension Returnable {
func returnValue(value: T) -> T {
return value
}
}
struct AStruct: Returnable {}
AStruct().returnValue(3) // default signature: Int -> Int
You get the function for free only by conforming to the protocol without specifying the type of T. If you want to set your own type write typealias T = String // or any other type in the struct body.
Some additional notes about the provided code example
You solved the problem because you made it explicit which type the parameter has. Swift also infers your used type:
class SomeClass: SomeProtocol {
func someFunc(someVar: Double) {
print(someVar)
}
}
So SomeType of the protocol is inferred to be Double.
Another example where you can see that SomeType in the class declaration doesn't refer to to the protocol:
class SomeClass: SomeProtocol {
typealias Some = Int
func someFunc(someVar: Some) {
print(someVar)
}
}
// check the type of SomeType of the protocol
// dynamicType returns the current type and SomeType is a property of it
SomeClass().dynamicType.SomeType.self // Int.Type
// SomeType gets inferred form the function signature
However if you do something like that:
protocol SomeProtocol {
typealias SomeType: SomeProtocol
func someFunc(someVar: SomeType)
}
SomeType has to be of type SomeProtocol which can be used for more explicit abstraction and more static code whereas this:
protocol SomeProtocol {
func someFunc(someVar: SomeProtocol)
}
would be dynamically dispatched.
There is some great information in the documentation on "associated types" in protocols.
Their use is abundant throughout the standard library, for an example reference the SequenceType protocol, which declares a typealias for Generator (and specifies that it conforms to GeneratorType). This allows the protocol declaration to refer to that aliased type.
In your case, where you used typealias SomeType = Int, perhaps what you meant was "I want SomeType to be constrained to Integer-like behavior because my protocol methods will depend on that constraint" - in which case, you may want to use typealias SomeType: IntegerType in your protocol, and then in your class go on to assign a type to that alias which conforms to IntegerType.
UPDATE
After opening a bug w/ Apple on this and having had extensive discussion around it, I have come to an understanding of what the base issue is at the heart of this:
when conforming to a protocol, you cannot directly refer to an associated type that was declared only within that protocol
(note, however, that when extending a protocol the associated type is available, as you would expect)
So in your initial code example:
protocol SomeProtocol {
typealias SomeType = Int
func someFunc(someVar: SomeType)
}
class SomeClass: SomeProtocol {
func someFunc(someVar: SomeType) { // use of undeclared type "SomeType"
print(someVar)
}
}
...the error re: "use of undeclared type" is correct, your class SomeClass has not declared the type SomeType
However, an extension to SomeProtocol has access to the associated type, and can refer to it when providing an implementation:
(note that this requires using a where clause in order to define the requirement on the associated type)
protocol SomeProtocol {
typealias SomeType = Int
func someFunc(someVar: SomeType)
}
extension SomeProtocol where SomeType == Int {
func someFunc(someVar: SomeType) {
print("1 + \(someVar) = \(1 + someVar)")
}
}
class SomeClass: SomeProtocol {}
SomeClass().someFunc(3) // => "1 + 3 = 4"
There is great article that actually gives you answer for your question. I suggest everyone to read it to get into type-aliases and some more advanced stuff that comes up when you use it.
Citation from website:
Conceptually, there is no generic protocols in Swift. But by using
typealias we can declare a required alias for another type.