I am learning Elixir modeling a board game and I have this code:
defprotocol Board do def can_handle_move(self) def handle_move(self, move) end defimpl Board, for: List do def can_handle_move(self), do: Enum.empty?(self) def handle_move(self, move), do: List.delete(self, move) end
Implementation looks more complex than it really is. Actually, can_handle_move is just Enum.empty? and handle_move is just List.delete . Does Elixir have a way to express this? Sort of:
defimpl Board, for: List do def can_handle_move = &Enum.empty?/1 def handle_move = &List.delete/2 end
... which does not compile. I also tried without def s.
source share