Skip to content

Instantly share code, notes, and snippets.

@decors
Created January 26, 2017 04:35
Show Gist options
  • Save decors/a1ba2b5e4a700235b0d993378836d7b4 to your computer and use it in GitHub Desktop.
Save decors/a1ba2b5e4a700235b0d993378836d7b4 to your computer and use it in GitHub Desktop.
Crystal-lang simple delegator
class Delegator(T)
def initialize(@object : T)
end
def self.delegate(object)
new(object)
end
forward_missing_to @object
end
class Person
def greet
puts "Hi"
end
end
class PersonDelegator < Delegator(Person)
def greet_twice
2.times do greet end # call Person class method
end
end
person = Person.new()
person = PersonDelegator.delegate(person)
person.greet_twice # => Print "Hi" two times
person = Person.new()
person = Delegator(Person).new(person)
person.greet # "Hi"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment