arjan / decorator

Function decorators for Elixir
MIT License
385 stars 20 forks source link

[Question] is it possible to stack decorators? #32

Closed sebastialonso closed 5 years ago

sebastialonso commented 5 years ago

First of all, thank you for this neat library.

Just out of curiosity, is the current implementation able to stack and execute several decorators for a single function?

arjan commented 5 years ago

Maybe it's not really explicit from the docs, but having a function decorated multiple times actually works:

  @decorate innerdecorator()
  @decorate middledecorator()
  @decorate outerdecorator(:arg)
  def func1(x) do
    x
  end

I'm noticing some warnings being generated sometimes, I'll see what I can do about those

sebastialonso commented 5 years ago

Thanks @arjan!. After trying some examples I have found that decorators are indeed stackable.

In your decorators, does outerdecorator "calls" middledecorator and innerdecorator? More generally, is the inner most decorator called many times? I'm just lazy printing flags and they are inconsistent most of the times.

arjan commented 5 years ago

No, they just "chain". So the body that the middle decorator gets is the original body with the innerdecorator applied; likewise, the body that the outer decorator gets is middle(inner(body))

arjan commented 5 years ago

I think "composable" is a better word for this behaviour. Remember, in runtime all of this is gone.

sebastialonso commented 5 years ago

Awesome, thanks!