Thanks for the proposal.

I like has_attribute?. I prefer it over attribute_registered? because what
would happen if you set an attribute without registered it? Should it
return false for attribute_registetered? So has_attribute? is less
ambiguous, although I would only support has_attribute?/2 for now.

Notice in order to implement this, you will have to change
register_attribute to store something when called without the :persist or
:accumulate. But it should be a matter of adding an else here:
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/module.ex#L1308-L1311

Something like:

else
  :ets.insert_new(set, {attribute, nil, nil})
end

We need to document that once an attribute has been registered to
accumulate or persist, it can't be done undone unless the attribute is
deleted.

*José Valim*
www.plataformatec.com.br
Skype: jv.ptec
Founder and Director of R&D


On Tue, Jul 2, 2019 at 5:42 AM Allen Madsen <[email protected]> wrote:

> In working with the ExUnit code, there are at least two places where I
> think being able to introspect on module attributes would be useful.
>
> One is here
> <https://github.com/elixir-lang/elixir/blob/master/lib/ex_unit/lib/ex_unit/case.ex#L227-L233>.
> In this code it would be useful to know if a module attribute has been set
> already. This code checks to see if it has a value. However, if a user set
> the value to `nil`, this code wouldn't be able to tell.
>
> Another situation it would be useful, is in this PR:
> https://github.com/elixir-lang/elixir/pull/9181. Specifically, this check
> <https://github.com/elixir-lang/elixir/blob/e24a18338ef908aba8bf83e315d6e3938dbefce7/lib/ex_unit/lib/ex_unit/case.ex#L427-L431>,
> where it would also be useful to know if the attribute is registered with
> accumulate or not.
>
> So, the proposal is an API something like this:
>
> defmodule X do
>   Module.register_attribute(__MODULE__, :foo, accumulate: true)
>   @bar true
>
>   Module.attribute_registered?(__MODULE__, :foo) #=> true
>   Module.attribute_registered?(__MODULE__, :foo, accumulate: true) #=> true
>   Module.attribute_registered?(__MODULE__, :foo, persist: true) #=> false
>
>   Module.attribute_registered?(__MODULE__, :bar) #=> true
>   Module.attribute_registered?(__MODULE__, :bar, accumulate: true) #=>
> false
>
>   Module.attribute_registered?(__MODULE__, :baz) #=> false
> end
>
> The third argument would be a subset match of the options.
>
> Thoughts?
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/39c87e02-8ed2-49ed-9754-d0e8f0bafd7e%40googlegroups.com
> <https://groups.google.com/d/msgid/elixir-lang-core/39c87e02-8ed2-49ed-9754-d0e8f0bafd7e%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2Bb4afMwPpQP6Jv%3D_bCWJvRskNd%3DJJA_OHArJMH7621kg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to