Elixir 1.6

Chapter 9 

An Aside —
What Are Types?

Primitive Types Vs Modules

Primitive types are not necessarily the same as the types they represent.

The primitive Elixir list is just an ordered group of values.

The List module provides a set of functions that operate on lists.

These are different!

 

The primitive list is an implementation, whereas the List module adds a layer of abstraction.

Primitive Types Vs Modules

​iex>​ options = [ {​:width​, 72}, {​:style​, ​"​​light"​}, {​:style​, ​"​​print"​} ]
​[width: 72, style: "light", style: "print"]
​iex>​ List.last options
​{:style, "print"}
​iex>​ Keyword.get_values options, ​:style​
​["light", "print"]

Maps are also a primitive type.
And, like lists, they have an Elixir module that implements a richer, derived map type.

 

The Keyword type is an Elixir module. But it is implemented as a list of tuples:

​options = [ {​:width​, 72}, {​:style​, ​"​​light"​}, {​:style​, ​"​​print"​} ]

But Elixir adds functionality to give you a dictionary like behavior

Primitive Types Vs Modules

In a way, this is a form of the duck typing. The Keyword module doesn’t have an underlying primitive data type. It assumes that any value it works on is a list that has been structured a certain way.

Thank you!

Programming Elixir 1.6 Chapter 09

By Dustin McCraw

Programming Elixir 1.6 Chapter 09

  • 996