O'Reilly logo

Object–Oriented Programming with Swift 2 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

Generalizing existing classes with generics

In Chapter 3, Encapsulation of Data with Properties, we created a class to represent a mutable 3D vector named MutableVector3D and a class to represent an immutable version of a 3D vector named ImmutableVector3D.

Both versions were capable of working with 3D vectors with Float values for x, y, and z. We now realize that we also have to work with 3D vectors with Double values for x, y, and z in both classes. We definitely don't want to create two new classes, such as MutableDoubleVector3D and ImmutableDoubleVector3D. We can take advantage of generics to create two classes capable of working with elements of any floating point type supported in Swift—that is, either Float, Float80, or Double.

We want to ...

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