In recent versions of Elixir, Access is no longer implemented as a protocol, but as a behaviour. From what I gather, this change was motivated by performance considerations in development mode.
Now, looking at the implementation, I wonder how this works internally and why this implementation was chosen. As we can see here, Access dispatches to a struct's module via the underlying map's "__struct__" key. AFAICS, this roughly works like OOP-style polymorphism. A few questions about this:
- Why is this faster?
- What are the downsides compared to Protocols? From what I can tell it is less extensible. Are there other?
- So far I have only seen behaviours in the context of stuff like GenServer, where the callback module is captured at initialization and kept in a process (At least I assume as much). Here, the Access behaviour grabs the callback module from the data. Would it even be possible to implement this behaviour for something that is not a struct?
- Is this kind of dispatch a common best practice in Erlang or Elixir when one is not interested in the added benefit that a Protocol would give?
question from:
https://stackoverflow.com/questions/33704618/why-is-elixirs-access-behaviour-not-a-protocol 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…