elixir - Implementing a protocol by delegating to existing functions -
i'm learning elixir modeling board game, , have code:
defprotocol board def can_handle_move(self) def handle_move(self, move) end defimpl board, for: list def can_handle_move(self), do: enum.empty?(self) def handle_move(self, move), do: list.delete(self, move) end
the implementation looks more complicated is. actually, can_handle_move
enum.empty?
, handle_move
list.delete
. elixir have way of expressing this? like:
defimpl board, for: list def can_handle_move = &enum.empty?/1 def handle_move = &list.delete/2 end
...which doesn't compile. i've tried without def
s.
try kernel#defdelegate/2
it.
defimpl board, for: list defdelegate can_handle_move(self), to: enum, as: :empty? defdelegate handle_move(self, move), to: list, as: :delete end
Comments
Post a Comment