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 defs.

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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -