Extending base types to conform to custom protocols
Now, we want to be able to use any of the integer types as types in our MutableVector3D<T>
and ImmutableVector3D<T>
classes. We want to make the two classes capable of working with elements of any integer type supported in Swift, that is, any of the following types, in addition to the floating point types that the classes already support:
Int
Int16
Int32
Int64
Int8
UInt
UInt16
UInt32
UInt64
UInt8
It seems to be a pretty simple task. We would just have to replace the generic type constraint in each class declaration from FloatingPoint
to a more generic protocol. We need a protocol to which all the previously enumerated types conform to, and to which the floating point types also conform. However, we will ...
Get Swift 3 ObjectOriented Programming - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.