Zoltan Varadi
Zoltan Varadi

Reputation: 2488

Swift protocol oriented programming, resolving naming collision

I played a little with Protocol Oriented programming and ran into a case that could be quite common, but i don't know how it could be resolved. Below i have 2 protocols, both require a var named param to be present if the class intends to implement the protocol. But in the protocols, the var param are 2 different types. So how can i implement both without having to make change the protocols? Here's the code:

protocol firstProtocol
{
    var param:Int { get set }
}

protocol secondProtocol
{
    var param:String { get set }
}

class protocolImplementer: firstProtocol, secondProtocol
{
    var param:String = "foo"
    var param:Int = 0 // Invalid redeclaration of 'param'
}

Upvotes: 2

Views: 404

Answers (1)

Lubos
Lubos

Reputation: 1209

You cannot. A class cannot have 2 variables with the same name. How could the compiler decide, which variable are you referencing when calling from somewhere ? It is possible with the methods though in case they have different parameters.

Upvotes: 1

Related Questions