The Power of Generics : Swift

The Power of Generics : Swift

One of the most powerful features introduced in Swift was Generics. Generics are used to avoid duplication and to provide abstraction.

The generic code allows you to write flexible, reusable functions and data types that can work with any type that matches the defined constraints.

Swift standard libraries are built with generics code. Swift’s ‘Array’ and ‘Dictionary’ types belong to generic collections.

We will learn below things in the article,

  • Writing a Generic Data Structure
  • Writing a Generic Function
  • Extending a Generic Type
  • Constraining a Generic Type
  • Generics using the where clause

Writing a Generic Data Structure:

A queue is a data structure like a list or a stack, but one to which you can only add new values to the end (enqueue them) and only take values from the front (dequeue them).

Queue is a generic type with a type argument, element in its generic argument clause. Another way to say this is, Queue is generic over type Element. For example, Queue<Int> and Queue<String> will become concrete types of their own at runtime, which can only enqueue and dequeue strings and integers, respectively.

Writing a Generic Function:

Typically when we pass typed parameters in a function we do something like,

Now we can easily use generic parameters instead of creating a different function for each type.

For creating a generic function you need to set a placeholder value after the function name in angular brackets as: <Element>.

You need to use the same placeholder value as parameters/return types.

You can pass more than one placeholder values too.

Typically, if the generic parameter placeholder doesn’t represent anything, use T, U, V etc.

Extending a Generic Type:

Extending the queue to know the top of the item is included with the ‘extension’ keyword.

Constraining a Generic Type:

We can constrain a Generic type to conform to a certain type also.

Here the != won’t work without the Equatable protocol conformance.

Generics using the where clause:

We can use a where clause for an even stricter Generic type constraint checking. In the where clause we can add additional conditions.

So in the above code, we add a checker wherein the type T must conform to Military protocol as well besides conforming to the class Person.

Hence it’ll take only types of class AnotherPerson in the above code.

Swift generics are at the core of many common language features, such as arrays and optional. You’ve seen how to use them to build elegant, reusable code.

Generics in Swift is an integral feature that you’ll use every day to write powerful and type-safe abstractions.

 

 

 

 

The following two tabs change content below.
I'm a software developer at DEV IT, keen to learn new technologies to develop scalable as well as high-performance software and work towards challenging roles in the field of Information Technology is something that I always look forward to.

Latest posts by Harsh Mehta (see all)

One thought on “The Power of Generics : Swift

Leave a Reply

Your email address will not be published. Required fields are marked *