Slide 1

Slide 1 text

OOP in Elixir Wojtek Mach

Slide 2

Slide 2 text

defmodule Person do defstruct [:name] end joe = %Person{name: "Joe"} robert = %Person{name: "Robert"} joe.name # => "Joe" robert.name # => "Robert"

Slide 3

Slide 3 text

defmodule Person do defstruct [:name] def say_hello_to(from, to) do IO.puts("#{from.name}: Hello, #{to.name}") end end joe = %Person{name: "Joe"} robert = %Person{name: "Robert"} joe.name # => "Joe" robert.name # => "Robert" Person.say_hello_to(joe, robert) # => Joe: Hello, Robert

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

import OOP

Slide 6

Slide 6 text

import OOP class Person do var :name end

Slide 7

Slide 7 text

import OOP class Person do var :name end joe = Person.new(name: "Joe") robert = Person.new(name: "Robert")

Slide 8

Slide 8 text

import OOP class Person do var :name end joe = Person.new(name: "Joe") robert = Person.new(name: "Robert") joe.name() # => "Joe" robert.name # => "Robert"

Slide 9

Slide 9 text

import OOP class Person do var :name def say_hello_to(who) do IO.puts("#{this.name}: #{who.name}") end end joe = Person.new(name: "Joe") robert = Person.new(name: "Robert") joe.name() # => "Joe" robert.name # => "Robert" joe.say_hello_to(robert) # => Joe: Hello, Robert

Slide 10

Slide 10 text

import class var def end end joe robert joe.name() # => "Joe" robert.name # => "Robert" joe.say_hello_to(robert) # => Joe: Hello, Robert this.name

Slide 11

Slide 11 text

joe = Person.new(name: "Joe") joe.set_name("Hipster Joe") # => :ok joe.name # => "Hipster Joe"

Slide 12

Slide 12 text

Q: How does it work?


Slide 13

Slide 13 text

Q: How does it work?
 A: You don’t want to know.

Slide 14

Slide 14 text

Q: How does it work?
 A: Macros.

Slide 15

Slide 15 text

Perhaps it’s not that bad…


Slide 16

Slide 16 text

Q: What’s worse than OOP?


Slide 17

Slide 17 text

Q: What’s worse than OOP?
 A: OOP with inheritance.

Slide 18

Slide 18 text

import OOP class Human do var :name end

Slide 19

Slide 19 text

import OOP class Human do var :name end class Doctor < Human do end

Slide 20

Slide 20 text

import OOP class Human do var :name end class Doctor < Human do def title do "Dr. #{name}" end end

Slide 21

Slide 21 text

import OOP class Human do var :name end class Doctor < Human do def title do "Dr. #{name}" end end dr = Doctor.new(name: "Jekyll") dr.title # => "Dr. Jekyll"

Slide 22

Slide 22 text

Perhaps it’s not that bad…


Slide 23

Slide 23 text

Q: What’s worse than OOP with inheritance?


Slide 24

Slide 24 text

Q: What’s worse than OOP with inheritance?
 A: OOP with multiple inheritance.

Slide 25

Slide 25 text

import OOP class Human do end

Slide 26

Slide 26 text

import OOP class Human do end class Spider do end

Slide 27

Slide 27 text

import OOP class Human do end class Spider do end class Spiderman < [Human, Spider] do end

Slide 28

Slide 28 text

Elixir Guidelines

Slide 29

Slide 29 text

Q: What’s the 1st rule of writing macros?
 Elixir Guidelines

Slide 30

Slide 30 text

Q: What’s the 1st rule of writing macros?
 A: Don’t write macros. Elixir Guidelines

Slide 31

Slide 31 text

Q: What’s the 1st rule of doing OOP in Elixir?
 Elixir Guidelines

Slide 32

Slide 32 text

Q: What’s the 1st rule of doing OOP in Elixir?
 A: Don’t do OOP in Elixir. Elixir Guidelines

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

• Elixir is powerful
 
 
 
 
 


Slide 35

Slide 35 text

• Elixir is powerful • DSLs, abstractions, semantics
 
 
 


Slide 36

Slide 36 text

• Elixir is powerful • DSLs, abstractions, semantics • Complexity
 


Slide 37

Slide 37 text

• Elixir is powerful • DSLs, abstractions, semantics • Complexity • Simplicty

Slide 38

Slide 38 text

Thank you! @wojtekmach wojtekmach/oop