Generics — What is this “Element” parameter I keep seeing? (swift 3)

Doug Galante
5 min readOct 21, 2016

--

As I have been learning Swift I have noticed a few functions ask for a parameter of type Element. I was kind of confused by why we were being asked for a variable of type I have never heard of within something so familiar like an Array…

For example, lets take a look at the append() method called on a String Array.

When we use autofill to complete the append function we are asked for one argument: newElement: Element. Since we already know the array contains Strings, why isn’t the parameter of type String?

The append method will accept a String and print the method with the new string added to the end of the Array. So, whats going on here? Why am I being asked to give the method an Element but it will accept a String?

Lets think about Arrays for a second. Every Array we create has the append() method available, but not all Arrays are of the same type. Rather than use append() on a String Array, lets try it on an Int Array.

Nice! even though our array type has changed from [String] to [Int] we can use the Element type to pass a parameter of either String or Int. Since the type of the items in the array are changing, using Element allows us to pass in the correct type according to whats in our array.

But still.. what is Element? Is it a class? Struct? Turns out its realted to something called “Generic Code”. Here it is straight from the Apple Documentation:

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

Placeholder Type

The word Element relative to Generics is a type parameter that acts as a placeholder type. Instead of specifying a type we can use a placeholder that allows the type to be anything.

Lets take a look at the syntax of a normal function and a function using Generics.

Here is a function that takes a string and returns the string as a single item in an array. We can see that the function is limited only to taking strings and returning a string array.

In the second example we can see Generic code allowing the same method to take in a parameter of any type and return an array of any type. This gives the function a substantial increase in functionality.

Syntax

Lets take a look at the exact differences between these two functions and what makes the Generic version work.

First off you should notice that the placeholder type is declared immediately after the function name in angle brackets. In the previous examples we have used the word “Element” but lets switch it up here. You can name this anything you want but it should be indicative of what you are looking for as an argument. Lets use <T> for type.

Next, you can see the placeholder type is used to declare the parameter type. Note that if you have multiple parameters within your function that are of the placeholder type, they must all conform to the same type when the function is being called. If you try to call the function with different types being passed to the placeholders parameters, you will see an error.

Finally we see the return variable also utilizing the placeholder type. If we are going to return an array we need to explicitly state the arrays type. Since we don’t know what the type is yet we can use our placeholder type once again to work as a… placeholder.

Now when we auto-complete createArray() we can see it asking us for two values of type T. But now we know where the T came from!

And it works with different types!

We can also put multiple placeholder types between the angle brackets in our functions signature. They need to be separated by a comma and allow the function’s parameters to potentially take different types.

As a final note — Its good practice to name the parameter type to describe the relationship between the parameter and what its being used for. If you see <Element>, you should know you are working with an Array. With <Key> and <Value> you know you will be working with a dictionary.

If the parameter is not specific as to what its being used in — you can name it anything you want! Usually you will se single letters such as <T> or <V>.

--

--

Doug Galante

Aspiring Coder with a Fine Arts Degree ~ Making Time for Climbing, Cooking, Video Games, and Sleep.