In Python there are decorators. Can we do that some way in Elixir?
Assume we have defined it in the Decorater module, and We can use it like this:
use Decorator
defmodule M do
def tracter(module,fun_name,args) do
IO.puts("call #{fun_name}(#{args|> Enum.join(",")})")
ret = apply(module,fun_name,args)
IO.puts("get: #{inspect ret}")
ret
end
@wraper tracter
def add(a,b) do
a+b
end
end
a=M.add(1,2)
IO.puts("return is: #{a}")
The output shoule be:
call add(1,2)
get: 3
return is: 3