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

Advanced OOP in Elixir

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Advanced OOP in Elixir

Avatar for Wojtek Mach

Wojtek Mach

May 11, 2016

More Decks by Wojtek Mach

Other Decks in Programming

Transcript

  1. import OOP class Person do var :name end joe =

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

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

    IO.puts("#{this.name}: Hello, #{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
  4. import OOP class Person do var :name def say_hello_to(who) do

    IO.puts("#{this.name}: Hello, #{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 this.name
  5. class Shape do var :x var :y end class Rectangle

    < Shape do var :width var :height def area do this.width * this.height end end
  6. class Shape do var :x var :y end class Rectangle

    < Shape do var :width var :height def area do this.width * this.height end end class Square < Rectangle do def area do :math.pow(this.width, 2) end end
  7. abstract class ActiveRecord.Base do end class Post < ActiveRecord.Base do

    end class Comment < ActiveRecord.Base do end Post.new Comment.new
  8. abstract class ActiveRecord.Base do end class Post < ActiveRecord.Base do

    end class Comment < ActiveRecord.Base do end Post.new Comment.new ActiveRecord.Base.new ** (RuntimeError) cannot instantiate abstract class ActiveRecord.Base
  9. abstract class ActiveRecord.Base do end abstract class ApplicationRecord < ActiveRecord.Base

    do end class Post < ApplicationRecord do end class Comment < ApplicationRecord do end
  10. class HalfLife do end final class HalfLife2 < HalfLife do

    end class HalfLife3 < HalfLife2 do end ** (RuntimeError) cannot subclass final class HalfLife2
  11. class AppleInc do var :devices end apple = Apple.new(devices: ["Alice's

    iPhone", "Bob's iPad"]) apple.devices # => ["Alice's iPhone", "Bob's iPad"]
  12. class AppleInc do private_var :devices end apple = Apple.new(devices: ["Alice's

    iPhone", "Bob's iPad"]) apple.devices ** (RuntimeError) Cannot access private var devices
  13. class NSA do def get_data(company, _fisa_court_order \\ nil) do company.devices

    end end class AppleInc do private_var :devices end apple = Apple.new(devices: ["Alice's iPhone", "Bob's iPad”]) NSA.new.get_data(apple) ** (RuntimeError) Cannot access private var devices
  14. class NSA do def get_data(company, _fisa_court_order \\ nil) do company.devices

    end end class AppleInc do friend NSA private_var :devices end apple = Apple.new(devices: ["Alice's iPhone", "Bob's iPad”])
  15. class NSA do def get_data(company, _fisa_court_order \\ nil) do company.devices

    end end class AppleInc do friend NSA private_var :devices end apple = Apple.new(devices: ["Alice's iPhone", "Bob's iPad”]) NSA.new.get_data(apple) # => ["Alice's iPhone", "Bob's iPad"]