O'Reilly logo

Swift 3 ObjectOriented Programming - Second Edition by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required