Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OOP in Elixir

Wojtek Mach
February 17, 2016
290

OOP in Elixir

Wojtek Mach

February 17, 2016
Tweet

Transcript

  1. defmodule Person do defstruct [:name] end joe = %Person{name: "Joe"}

    robert = %Person{name: "Robert"} joe.name # => "Joe" robert.name # => "Robert"
  2. 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
  3. import OOP class Person do var :name end joe =

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

    Person.new(name: "Joe") robert = Person.new(name: "Robert") joe.name() # => "Joe" robert.name # => "Robert"
  5. 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
  6. import class var def end end joe robert joe.name() #

    => "Joe" robert.name # => "Robert" joe.say_hello_to(robert) # => Joe: Hello, Robert this.name
  7. import OOP class Human do var :name end class Doctor

    < Human do def title do "Dr. #{name}" end end
  8. 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"
  9. import OOP class Human do end class Spider do end

    class Spiderman < [Human, Spider] do end
  10. Q: What’s the 1st rule of doing OOP in Elixir?


    A: Don’t do OOP in Elixir. Elixir Guidelines