# Function-specific attribute values

**URL:** <https://forum.elixirforum.com/t/function-specific-attribute-values/949>\
**Category:** Questions\
**Created:** [June 27, 2016, 7:07pm UTC](https://forum.elixirforum.com/t/function-specific-attribute-values/949 "2016-06-27T19:07:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmdd](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmdd/32/1859_2.png) [@cmdd](https://forum.elixirforum.com/u/cmdd)\
**Post date:** [June 27, 2016, 7:07pm UTC](https://forum.elixirforum.com/t/function-specific-attribute-values/949/1 "2016-06-27T19:07:16Z")

</div>

Hello,

I was wondering if there was any way of defining an attribute whose value only applies to a following function definition, like `@doc` does. So far, all of the documentation I’ve been reading have only dealt with attributes whose values apply module-wide, and I was wondering if creating an attribute like `@doc` would even be possible.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Onor.io](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/onor.io/32/960_2.png) [@Onor.io](https://forum.elixirforum.com/u/Onor.io)\
**Post date:** [June 27, 2016, 7:33pm UTC](https://forum.elixirforum.com/t/function-specific-attribute-values/949/3 "2016-06-27T19:33:55Z")

</div>

I don’t quite follow the use case here. If you need some value specific to a function, then why not just declare the value in the function? I mean, for example, let’s say I needed a tax rate and I only wanted it visible to a particular function definition. What’s wrong with this:

```
def calculate_tax_rate(amount) do
  tax_rate = 1.06 #Yay Michigan!
  amount * tax_rate
end

```

I’m not sure how having an `@tax_rate attribute` (assuming it’s only used in `calculate_tax_rate`) would be a significant improvement. Can you detail your use case a bit more?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [June 27, 2016, 9:15pm UTC](https://forum.elixirforum.com/t/function-specific-attribute-values/949/4 "2016-06-27T21:15:23Z")

</div>

There are plenty of use cases, one was already mentioned, `@doc` also one could think of a `@deprecated <version>` which wanrs when the funtion following this annotation is still in the code when we compile that module and the applications version in `mix.exs` is greater than or equal to `<version>`.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [June 27, 2016, 9:22pm UTC](https://forum.elixirforum.com/t/function-specific-attribute-values/949/5 "2016-06-27T21:22:24Z")

</div>

I think you are searching for the module attribute `@on_definition <module>`.

After having this set, `<module>. __on_definition__ /6` will get called on every `def`, `defp`, `defmacro`, defmacrop` and maybe others.

An example head, I have floating around from experimenting with that attribute is ` def __on_definition__ (env, kind, _name, args, _guards, _body)`

As far as I remember `env` is the environment the `def` is in while `kind` is the keyword invoking this definition (`def`, `defmacro` etc). `name` is an atom containing the name of the defined item, `args`, `guards` and `body` are the ASTs of the respective parts of the definition. But I haven’t tested so far how it behaves when there are multi-headed-functions.

In you ` __on_definition__ /6` there you can query your specific attribut, manipulate its value and also delete it again. Most functions useful for this are living in `Module` IIRC.

* * *

A small example which is meant to manipulate the content of `@doc` is at [exygen/lib/exygen/compiler.ex at master · NobbZ/exygen · GitHub](https://github.com/NobbZ/exygen/blob/master/lib/exygen/compiler.ex)

Perhaps I’ll find time to continue after exam phase

---

<div class="post-metadata">

**Author:** ![Onor.io](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/onor.io/32/960_2.png) [@Onor.io](https://forum.elixirforum.com/u/Onor.io)\
**Post date:** [June 28, 2016, 12:26pm UTC](https://forum.elixirforum.com/t/function-specific-attribute-values/949/6 "2016-06-28T12:26:17Z")

</div>

Oh I see–so using attributes as a decoration for a function. Ok, that’s a use case I just hadn’t considered.

---

<div class="post-metadata">

**Author:** ![cmdd](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmdd/32/1859_2.png) [@cmdd](https://forum.elixirforum.com/u/cmdd)\
**Post date:** [June 29, 2016, 6:27pm UTC](https://forum.elixirforum.com/t/function-specific-attribute-values/949/7 "2016-06-29T18:27:40Z")

</div>

Thanks to everyone for the responses! I will try the ` __on__ definition__` solution out.

---

<div class="post-metadata">

**Author:** ![arjan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/arjan/32/2737_2.png) [@arjan](https://forum.elixirforum.com/u/arjan)\
**Post date:** [October 20, 2016, 6:51am UTC](https://forum.elixirforum.com/t/function-specific-attribute-values/949/8 "2016-10-20T06:51:28Z")

</div>

Did you find anything? I’m looking for the same thing, see [this topic](https://forum.elixirforum.com/t/nicest-way-to-emulate-function-decorators/2050/2).
