An interesting conversation was struct up on erlanger slack around accessing elements in a record via pattern matching without knowing the record type. The TL;DR of that part it that this is map functionality and not really possible with records AFAIK.
This led to the thought of having something similar to structs in elixir, in erlang, but faster
Basically, a would-be struct could be built on top of a record or similar to a record for performance reasons.
A nicety that structs in elixir allow for is optionally matching on the struct name or just treating it like a plain ole map, that’s per a struct being a map under the hood. What’s more, they don’t require importing the definition of a struct, the compiler figures this out per a struct attribute which points back to the defining module.
Thus, I think it would be possible to have a similar structure and behaviour to go along with it, but with record behavior underneath.
If I think through the compiler steps, it should be possible, although there would be a cost associated with it at compile time. That is, instead of simply checking the struct definition to see if a key is or is not part of the definition, in erlang and with something like a record underneath, the pattern match or access method could expand to grabbing the desire element out of the struct (tuple) at it’s precise location per looking up the definition (grabbing it from the module that defined it).
Aside from having to restrict structs to being defined on top of a module, the other con would be that spitting out a tuple isn’t the most friendly format if you’re debugging, but perhaps it could be pretty printed in such a way that is friendly.
Quite interested what people think of this.
I think the friendly formatting might be able to be solved with a bit of abstraction, that is one could define the structure using a map, but under the hood it ends up being a tuple, and when pretty printing, you can just refer back to the map to and spit it out on the screen as map, but perhaps with a
module# prefix or something similar.