protocol Nameable: Equatable {
var name: String { get set }
}
struct Person: Nameable {
var name: String
}
struct Car: Nameable {
var name: String
}
Now I can constrain a function to be able to print elements conforming to the Nameable
protocol since they are of the same type?
func printNameable<T: Nameable>(_ list: [T]) {
}
But how I can create this list? I get a syntax Error.
var nameableList<T: Nameable>: [T] = []
Or if I try next I get Protocol 'Nameable' can only be used as a generic constraint ...
var nameables: [Nameable] = []
I am forced to specify a Nameable type like Person or Car to create this list?
Basically, in the end, I want an array of a common protocol that can have a variety of different type objects that I can compare, if they are of different type they are different, but if not, I want to compare them.
var nameables: [Nameable] = [Person(name: "John"), Car(name: "Ferrari")]
nameables.contains(Car(name: "Ferrari"))
(I guess both types, Person and Car should conform Equatable protocol).