Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 983 of the free egghead.io lessons, plus get Elm content delivered directly to your inbox!

Existing egghead members will not see this. Sign in.

Reuse Functions Through Type Variables in Elm

1:43 Elm lesson by

Sometimes a function is useful for many different types. Instead of writing a copy of that function for each different type, learn to use type variables and allow the same function to be reused.

egghead.io comment guidelines


Sometimes a function is useful for multiple different types. Instead of writing a copy of that function for each different type, learn to use type variables and allow the same function to be reused, instead.


Which editor are you using?


Hi, Marshallformula!

I'm using Atom in these videos, with this general setup: https://gist.github.com/splodingsocks/cec8f09f8d2d0bd11afd4302e33e1684

In reply to marshallformula


In reply to Murphy

Let's make a function that'll allow us to print these numbers to the screen. It's going to take an int and it's going to return HTML message. Don't really worry about what message means right now. That's there for building interactive applications.

Let's define the body of this function. Print numbers is going to take in an integer and it's going to return NUL with no attributes and then we'll put a text note in here, and we'll pass in two string of the int that we get in. Now down here, instead of this list of children, we can put in some parens and call it list.map print numbers, numbers.

OK, so we've got a list of numbers in the browser. Now, let's say we wanted to print out the fruits instead of the numbers. We can't call print numbers with fruits because it's expecting an integer and fruits is a list of objects, but we look at the code inside of here, and this should all be pretty generic. All we're doing is doing a two string and then putting that into a text note.

This actually should be adaptable between numbers and objects. Let's make adaptable by changing this upper case int type to a lower case thing. It doesn't matter what word I use here. What matters is that it's lower case. When Elm sees a lower case word inside of a type signature, it knows that that's a variable, that that can change depending upon the context.

Just to make things make sense, let's change print numbers now to be instead print thing, and let's change this int argument to be thing. Down here we'll have to change the reference to print thing. Let's make sure it still works in the browser. It does. What if we pass in fruits? It works. Here we've defined a function called print thing that takes in any type and does something to it.

We know it can take in any type because it's got a lower case word instead of an upper case word as its type, and we've been able to call it with both a list of integers and a list of objects.

Joel's Head
Why are we asking?