I am playing around with Swift and am stumbling over the following problem: given I have the predefined class Animal
:
//Predefined classes
class
<>
is in Objective-C: conforms to protocol
<>
is in Swift for generics
You can do more with the where
keyword
Use where after the type name to specify a list of requirements—for example, to require the type to implement a protocol, to require two types to be the same, or to require a class to have a particular superclass.
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.
To me, this seems more an architecture problem:
Nameable
is strange as a protocol. Logically, every Animal has the ability to be named so Animal
should always conform to Nameable
It would be much simpler to allow for nil
names when the animal is not named instead of having animals that can have a name and animals that can't.
Then you can enforce names in Zoo
simply by an assert
.
You can use a generic with a where
clause with multiple conditions.
func addAnimal<T: Animal where T: Nameable>(animal: T) { ... }
Amendment: You should probably make the whole class this generic so you can type the array properly
class Zoo<T: Animal where T: Nameable> {
var animals : T[] = []
func addAnimal(a: T) {
...
}
}
Swift 3 version
In Swift 3 the structure changed a bit. That´s why you will get a deprecation warning for the old structure. Here´s the new one:
For functions the expected protocols are located after the parameters definition of the function. Example:
func addAnimal<T: Animal>(animal: T) where T: Nameable
For enums or classes the structure also changed
enum ZooEnum<T: Animal> where T: Nameable
class Zoo<T: Animal> where T: Nameable