Skip to content

Instantly share code, notes, and snippets.

@xqyww123
Created May 25, 2018 07:58
Show Gist options
  • Save xqyww123/bc1b90cecbf46b5af15aceae503127c6 to your computer and use it in GitHub Desktop.
Save xqyww123/bc1b90cecbf46b5af15aceae503127c6 to your computer and use it in GitHub Desktop.
class_warpper.cr
lib LibPet
alias Pet = Void*
alias Dog = Void*
fun make_dog : Dog
fun bark(bar : Dog)
fun move(pet : Pet)
fun retreat(pet : Pet)
fun sell(pet : Pet)
end
alias Dog = lib::Dog
struct Dog
def self.new
LibPet.make_dog
end
def bark
LibPet.bark(self)
end
def move
LibPet.move(self)
end
def retreat
LibPet.retreat(self)
end
# or macro:
# def_delgators LibPet, :bark, :move, :retreat
def at_front
move
yield
ensure
retreat
end
def attack
at_front { bark }
end
end
# So That:
def sell_dog(dog : Dog)
LibPet.sell_pet dog
# rather than
# LibPet.sell_pet dog.dog
# `dog.dog` is too silly, don't it?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment