Use the @types, Luke

A presentation at VanBEAM in March 2016 in Vancouver, BC, Canada by Brooklyn Zelenka

Slide 1

Slide 1

BROOKLYN ZELENKA USE THE @TYPES, LUKE

Slide 2

Slide 2

BROOKLYN ZELENKA SHAMELESS SELF PROMOTION. SHAMELESS. ▸ Organizes C&C, VanFP, VanEE ▸ Last VanEE in person ▸ Co-founder of Robot Overlord Inc, developer at MetaLab ▸ Author of Quark, Algae, and Witchcraft

Slide 3

Slide 3

USE THE @TYPES, LUKE WHAT WE’RE GOING TO COVER ▸ Why types have a bad rep ▸ Elixir’s type system ▸ Typespecs ▸ Dialyxir ▸ Defining our own types ▸ Structs ▸ Parametric polymorphism ▸ Q&A

Slide 4

Slide 4

WHY TYPES HAVE A BAD REP

Slide 5

Slide 5

WHY TYPES HAVE A BAD REP THERE ARE TWO KINDS OF TYPE SYSTEMS. SOME ARE FOR THE COMPILER. OTHERS ARE FOR THE PROGRAMMER. Overheard at LambdaConf 2015

Slide 6

Slide 6

WHY TYPES HAVE A BAD REP TYPES FOR THE COMPILER (EX. C & JAVA) ▸ Boilerplate ▸ Don’t add to the code expressivity ▸ Source of (annoying) warnings

Slide 7

Slide 7

WHY TYPES HAVE A BAD REP TYPES FOR PROGRAMMERS (EX. HASKELL, ELIXIR, SWIFT) ▸ Annotate the meaning of a piece of code ▸ Help to structure your code ▸ Double as documentation ▸ Catch some bugs before you run your code!

Slide 8

Slide 8

ELIXIR’S TYPE SYSTEM

Slide 9

Slide 9

ELIXIR HAS “WEAK” DYNAMIC TYPES. “Weak” doesn’t imply bad

Slide 10

Slide 10

ELIXIR’S TYPE SYSTEM WEAK, DYNAMIC TYPES ▸ Type inference at run time ▸ Static analysis tools do exist (Dialyxr) ▸ Determine code behaviour through parametric polymorphism

Slide 11

Slide 11

ELIXIR’S TYPE SYSTEM BUILT-IN TYPES ▸ There’s quite a few ▸ Some contain others ▸ ex. integer is contained in number

Slide 12

Slide 12

TYPESPECS

Slide 13

Slide 13

TYPESPECS OPTIONAL, GRADUAL TYPING ▸ ex. @spec add(integer, integer) :: integer ▸ Similar syntax to @doc, etc. ▸ Lives outside of the function definition ▸ Has an “@“ before it ▸ Generates documentation

Slide 14

Slide 14

DIALYXIR (DIALYZER)

Slide 15

Slide 15

DIALYXIR (DIALYZER) DIALYZER IS A STATIC ANALYSIS TOOL THAT IDENTIFIES SOFTWARE DISCREPANCIES SUCH AS TYPE ERRORS, UNREACHABLE CODE, UNNECESSARY TESTS, ETC IN SINGLE ERLANG MODULES OR ENTIRE (SETS OF) APPLICATIONS. erlang.org

Slide 16

Slide 16

DIALYXIR (DIALYZER) HOW TO DIALYZE ▸ Add to mix.exs dependencies ▸ mix deps.get ▸ mix deps.compile ▸ mix dialyzer

Slide 17

Slide 17

DIALYXIR (DIALYZER) EXAMPLE OUTPUT

Slide 18

Slide 18

DEFINING OUR OWN TYPES

Slide 19

Slide 19

DEFINING OUR OWN TYPES CUSTOM TYPE EXAMPLE @type number_with_remark :: {number, String.t} @spec add(number, number) :: number_with_remark def add(x, y), do: {x + y, “You need a calculator to do that?”}

Slide 20

Slide 20

STRUCTS

Slide 21

Slide 21

STRUCTS STRUCT TYPES ▸ Multiple named fields ▸ Can get their own type defmodule Algae.Maybe do @type t :: Just.t | Nothing.t defmodule Nothing do @type t :: %Nothing{} defstruct [] end defmodule Just do @type t :: %Just{just: any} defstruct [:just] end end

Slide 22

Slide 22

PARAMETRIC POLYMORPHISM

Slide 23

Slide 23

PARAMETRIC POLYMORPHISM PROTOCOLS ▸ Give a definition of a function name per data type or struct “type” defimpl Witchcraft.Monoid, for: List do def identity(_list), do: [] def append(as, bs), do: as ++ bs end defimpl Witchcraft.Monoid, for: Map do def identity(_map), do: %{} def append(ma, mb), do: Dict.merge(ma, mb) end

Slide 24

Slide 24

Q&A TIME