cevado

cevado

EEP-79 native records and structs

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.

  1. 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 use Map.drop/2 on a struct and passes a bad structured data.
  2. 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:

  1. no support or half-baked support just as it happens with current records.
  2. new semantics for records/native-records meaning probably new syntax around it.
  3. 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.

Most Liked

garazdawi

garazdawi

Erlang Core Team

Native records does not check all fields, it only checks the fields you have listed + the module:record name.

From EEP-79:

When a native-record value is matched, its captured native-record definition is consulted.

So it behaves very similarly to how Structs except you cannot use the :maps API 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.

wojtekmach

wojtekmach

Hex Core Team

Citation needed for half-baked tuple record support. Record module exists for interoperability and I’m sure it could be improved. Have you tried submitting PRs for bugs or missing features?

derek-zhou

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.
.

Where Next?

Popular in Proposals: Ideas Top

dorgan
Currently the Phoenix.LiveView.JS struct is defined as an @opaque type, meaning it’s fields are internal api not to be relied upon. I ha...
New
Oliver
One common problem we face in constructing lists is that there is (AFAIK) no support for conditionally inserting members into list declar...
New
sevensidedmarble
I have a very simple suggestion: the generated config/dev.exs file should read from PORT at compile time to set the endpoints port. If ...
New
PJUllrich
Hey folks, I have the unique problem that I need to ignore all “change” and “input” events for one specific input element in a LiveView F...
New
superchris
Currently there is no out of the box way to support DOM Custom Events in phoenix. I’ve created a separate library to do this, but I’d lov...
New
Jskalc
Hi everyone! Recently I was thinking a lot about the way HEEX renders lists. People are generally surprised about huge payloads being sen...
New
zachdaniel
Something we do in various Ash packages is provide routes that the user forwards to in their own application. This is because we are deri...
New
markevans
Hi! I’m excited about everything that’s going on re. gradual typing and am really pleased to see that Jose and the team seem to be think...
New
jakeprem
Goal: To make JS.patch and JS.navigate more interoperable with JS.push. Scenario: Imagine making a reusable Phoenix component and you wa...
New
GenericJam
I’ve been messing around with image generation models recently and thought this could be wrapped in a library or people could just use th...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement