[Feature proposal] @enforce_keys should accumulate

40 views
Skip to first unread message

Krzysztof Wende

unread,
Mar 7, 2019, 7:52:13 AM3/7/19
to elixir-lang-core
In case we want to define a macro that requires some keys from the struct, @enforce_keys should have `accumulate: true`
Otherwise we cannot require some fields from a structures in multiple places.

We could use 
```
quote do
    @enforce_keys [:key] ++ (Module.get_attribute(__MODULE__, :enforce_keys) || [])
end
```
in __using__
But this way if we do `use` before our own @enforce_keys the key will get overwritten.
If we allowed that to accumulate we could do @enforce_keys many time still respecting all of them

José Valim

unread,
Mar 7, 2019, 8:20:11 AM3/7/19
to elixir-l...@googlegroups.com
This is tricky because I can see someone using @enforce_keys to define a default value and they may want to override it later on.

I think there is no right answer but given there is a cost of change, I would suggest to keep the behaviour as is.


José Valim
Skype: jv.ptec
Founder and Director of R&D


--
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 elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/213ebf4b-c4b3-4468-b191-efe9c203f403%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Allen Madsen

unread,
Mar 7, 2019, 9:55:23 AM3/7/19
to elixir-l...@googlegroups.com
You'd have to try this out, but it might work to add the additional enforced keys in __before_compile__ instead.

Reply all
Reply to author
Forward
0 new messages