Download - Use the @types, Luke
![Page 1: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/1.jpg)
USE THE @TYPES, LUKEBROOKLYN ZELENKA
![Page 2: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/2.jpg)
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
![Page 3: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/3.jpg)
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
![Page 4: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/4.jpg)
WHY TYPES HAVE A BAD REP
![Page 5: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/5.jpg)
THERE ARE TWO KINDS OF TYPE SYSTEMS. SOME ARE FOR THE COMPILER. OTHERS ARE FOR THE PROGRAMMER.
Overheard at LambdaConf 2015
WHY TYPES HAVE A BAD REP
![Page 6: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/6.jpg)
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
![Page 7: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/7.jpg)
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!
![Page 8: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/8.jpg)
ELIXIR’S TYPE SYSTEM
![Page 9: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/9.jpg)
ELIXIR HAS “WEAK” DYNAMIC TYPES.
“Weak” doesn’t imply bad
![Page 10: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/10.jpg)
ELIXIR’S TYPE SYSTEM
WEAK, DYNAMIC TYPES
▸ Type inference at run time
▸ Static analysis tools do exist (Dialyxr)
▸ Determine code behaviour through parametric polymorphism
![Page 11: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/11.jpg)
ELIXIR’S TYPE SYSTEM
BUILT-IN TYPES
▸ There’s quite a few
▸ Some contain others
▸ ex. integer is contained in number
![Page 12: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/12.jpg)
TYPESPECS
![Page 13: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/13.jpg)
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
![Page 14: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/14.jpg)
DIALYXIR (DIALYZER)
![Page 15: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/15.jpg)
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
DIALYXIR (DIALYZER)
![Page 16: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/16.jpg)
DIALYXIR (DIALYZER)
HOW TO DIALYZE
▸ Add to mix.exs dependencies
▸ mix deps.get
▸ mix deps.compile
▸ mix dialyzer
![Page 17: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/17.jpg)
DIALYXIR (DIALYZER)
EXAMPLE OUTPUT
![Page 18: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/18.jpg)
DEFINING OUR OWN TYPES
![Page 19: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/19.jpg)
DEFINING OUR OWN TYPES
CUSTOM TYPE EXAMPLE
@type number_with_remark :: {number, String.t}@spec add(number, number) :: number_with_remarkdef add(x, y), do: {x + y, "You need a calculator to do that?"}
![Page 20: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/20.jpg)
STRUCTS
![Page 21: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/21.jpg)
STRUCTS
STRUCT TYPES
▸ Multiple named fields
▸ Can get their own typedefmodule 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
![Page 22: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/22.jpg)
PARAMETRIC POLYMORPHISM
![Page 23: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/23.jpg)
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
![Page 24: Use the @types, Luke](https://reader034.vdocuments.mx/reader034/viewer/2022042907/58854f781a28abb5368b7b9d/html5/thumbnails/24.jpg)
Q&A TIME