cevado
I was reading the EEP-79, and thinking about the poor record support in Elixir(i’ve tried to discuss about that in the forum before). I see 2 clear advantages of native-records over structs, mainly it being a enforced natively by the runtime and the fact that it avoids creating modules just to define new data structures.
- on being enforced by the runtime:
currently there is a misguiding property on structs that guarding against a struct%MyStruct{}is actually just a validation on%{__struct__: MyStruct}. while with native-records it gonna be enforced that it have all fields. this makes native-records more reliable, so you don’t accidentally useMap.drop/2on a struct and passes a bad structured data. - on avoiding creating modules just to hold a data structure:
native-records are defined by#module.record{}, this allows to create multiple records in a single module, making it easy to handle and providing a better named scope for the data structure. this avoids the overuse of modules just to define a new struct.
I didn’t spot any mention to how native-records would work on distributed erlang and what would be the expected behavior on conflicting definitions when you send data from one node to another(that being one of my problems with relying too much on structs). but just those 2 points I think is a reason to prefer native-records.
I’m not sure if the core team is already discussing how to deal with native-records, but i’d really like it to be well supported by elixir(I still think named anonymous functions could be supported by elixir, for example, and I know I missed that train). So if the discussion is not supposed to be on closed doors, I’d like to start it here.
I personally see 3 possible ways to how it gonna unfold:
- no support or half-baked support just as it happens with current records.
- new semantics for records/native-records meaning probably new syntax around it.
- converge structs and native-records to be a single thing.
I really hope that 1 doesn’t happen, and 3 is too big of a heavy lift(even heavier if we consider the efforts on the type system) to be done right away. the more reasonable/sane approach would seem to be 2, maybe with converging both in a far future 2.0 elixir version.
but I really would like to know if there are any plans from the core team on that subject.
Trending in Proposals: Ideas
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
garazdawi
Native records does not check all fields, it only checks the fields you have listed + the module:record name.
From EEP-79:
So it behaves very similarly to how Structs except you cannot use the
:mapsAPI to manipulate it. This ties into how native records work with multiple versions, that is you can access old native records of the same module:record as long as you are only reading/changing fields that exist in it.I don’t know which way that Elixir will take with native-records as Elixir has a similar problem as Erlang’s tuple records in this regard, that is the implementation of structs has leaked through into user code and thus it is hard/impossible to do a switch without the user deciding that it is safe to switch.
michallepicki
Slightly related, Structs allow to match on the module name, but it is not yet possible with native records - there is an issue already Allow pattern matching (or guards) on native records module/name · Issue #10861 · erlang/otp · GitHub
Given that native records will be experimental in Erlang/OTP 29 (and maybe also in Erlang/OTP 30), it seems like this is the time to provide feedback like this
I hope converging both, and maybe it’s a good reason for 2.0 to not be that far in the future but as soon as native records are stable, in one or two years? If structs could be implemented as native records, it would improve interoperability with Erlang libraries (and maybe Gleam could adopt them as well?).
cevado
Unless I understood the EEP wrongly I guess it’s very different from structs given this:
so the moment you modify it to have a different shape, you gonna need to create a new data structure(being converting the native-record to map/proplist, or moving to a different native-record). structs even if you modify them, they keep being the same data structure(maps) and pass guards validation even if malformed data is given. I understand that elixir syntax sugars around structs try to guardrail against those malformed data, but just by being a new data structure native records have more guardrails around it then structs.
I agree, that’s why I think it would be good to discuss having it supported now and maybe in a very far future of elixir 2.0 have it converge. That’s my point to start the discussion. first highlighting how different it is from structs and why it’s good to have a first class support to it in Elixir even if we have structs.
cevado
Yep. for now I’d like to know what are the thoughts of Elixir core team on adding support to it, maybe making anonymous dot notation to work on it. and possibly access behavior implementation for it. probably around erlang 31 when the thing gets more solid.
i wouldn’t rush it. initially i just want it to not being ignored
wojtekmach
Citation needed for half-baked tuple record support.
Recordmodule exists for interoperability and I’m sure it could be improved. Have you tried submitting PRs for bugs or missing features?cevado
2 years ago I asked in this forum if there was a reason for elixir records to not behave the same way they behave on erlang. i didn’t open a bug for this bc if the way it is right now is desired, there is no reason to fix it(and I assume is desired bc it’s a pretty obvious issue on implementation side).
I’m not saying it’s half-baked as an offense to maintainers, but as a description of a thing that works kinda the same but with it’s own oddities to be functionally usable but without being fully part of the language(and I understand that its a design choice to make stuff simpler, more syntax would be conflicting with struct and map dot notation for example).
derek-zhou
If I need to define more than one struct in a module, I define them in bare modules that have nothing but defstruct and put all functions in the main module. The main module and the struct defining modules can still live in the same file, which is all that matters to me.
.
cevado
the problem is not creating files, and having a way to bypass creating a file doesn’t make it less of a problem.it reduces the friction, but doesn’t eliminate the problem that is: module name conflicting on being a function namespace or a data type identifier.
the two points I made was related to native records being a compelling feature that worth the effort of being fully brought to elixir(even if in a slow pace). i’m not trying to solve structs problems.
derek-zhou
Doesn’t reducing the friction make it less of a problem? Unless your definition of “problem” is not what I have. Do you have an example to show why “module name conflicting on being a function namespace or a data type identifier” has made it hard to express what you want to express in Elixir?
cevado
module name is a an atom, and in elixir it’s atom prefixed by
Elixir., atoms have a hard length limit on 255 characters. if you’re using module names not only as function namespace but also as a data type identifier, you have a tendency to add more stuff to that namespace, making it easier to offend the hard limit on 255 characters. and it can easily offended when people try to make structs describe deeply nested data.keeping module name as just a namespace, and making the record have it’s own name inside that namespace, stops the tendency to deeply nest one namespace in the other. you can describe the nested data in the same module with each nested level being it’s own independent record and composing on them(as it already happens with xmerl for example).