Swift pro tip of the day: Extension with generic where clause
Tags: swift, ios, beginners, learning
Published: 2018-09-07 20:33:35

---
title: Swift pro tip of the day: Extension with generic where clause
published: true
description: Combine Extensions and Generics and get powerful results
tags: swift, ios, beginners, learning
cover_image: https://thepracticaldev.s3.amazonaws.com/i/e26vi32wrzeagay9kwyq.png
---

Inspired by how successful was [my previous post](https://dev.to/eleazar0425/swift-computed-properties-in-a-nutshell-24ol) a few days ago, I'm writing a continuation with a better approach and in which I can introduce Swift features that are just as interesting as computed properties: ***extensions and generics***, and besides you'll notice how these two converge in a majestic combination. First of all let's quickly define what these two features are:

##Extensions:

> Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the source code (known as retroactive modeling). 

Extensions in Swift can:
* Add computed instance properties and computed type properties
* Define instance methods and type methods
* Provide new initializers
* Define subscripts
* Define and use new nested types
* Make an existing type conform to a protocol

[Source: The Swift Programming Language](https://docs.swift.org/swift-book/LanguageGuide/Extensions.html)

The most common sample could be to make a ViewController comfort a protocol using extensions: 

{% gist https://gist.github.com/eleazar0425/0060ec60a1c3b57de44aa239d6075118 %}


##Generic type:

> Generic code enables you to write flexible, reusable functions, and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.

> In addition to generic functions, Swift enables you to define your own generic types. These are custom classes, structures, and enumerations that can work with any type, in a similar way to Array and Dictionary.

[Source: The Swift Programming Language](https://docs.swift.org/swift-book/LanguageGuide/Generics.html#ID553)

Keep in mind that Arrays and Dictionaries are using Generic Types because that's going to be useful later. Do you remember the problem I was facing in my previous post? Let's recapitulate: I need the sum of selected pieces prices within a group of data. The approach I implemented with computed properties was like this:

{% gist https://gist.github.com/eleazar0425/acb9d9ea037676dd0a19357ed374a7d9 %}

There is another way to solve this problem using extensions and the ***generic where clause***. You can make an Array extension that requires the element to be a specific type or to conform to a protocol. So the above problem -using this combination- can be solved in the following way:

{% gist https://gist.github.com/eleazar0425/104b684ade20dda2180fc31989c29d87 %}

With the Where Clause, we're specifying that we want the Array Element to be type of Piece and then we are able to add a new property with the necessary logic. Same problem, solved from two different approaches taking advantage of Swift. I hope you enjoyed the post and if so, please follow me because more content will come soon. Goodbye!