Generics - Introduction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Introduction

Overview of This Module

In this module, you’ll learn more about generics, protocols, and functional programming. These tools enable you to use object-oriented principles and maintain type safety while keeping your code from growing too large.

Introduction to Generics

Swift is a type-safe language. You can’t even add an Int to a Double unless you convert one type to the other. The compiler keeps an eye on every line of code and flags any type mismatches. If you want to write a function or data structure that performs the same operations on different types of objects, it might seem that you must write the same code multiple times, once for each object type. But this would violate the DRY (Don’t Repeat Yourself) principle of software development. Fortunately, there’s a solution: Generic functions and types let you write code once and use it for different object types while maintaining type-safety.

See forum comments
Download course materials from Github
Next: Generics