NamedTuple vs Struct

Named tuples have some similarities with structs.

Both are value types, have a known size, allocated in the stack, and allow to access values by name, which might be more convenient than access by index of tuples. Named tuples are inmutable, structs can be inmutable too depending on the API you define. (This comparison is not exhaustive.)

Given a use case when a structure like that makes sense, would it be a good rule of thumb to choose named tuples when double splats are involved, and for anything else choose structs in principle?

1 Like

Yes, the main reason why named tuples exists is for named arguments / double splat.

For everything else its better to use a struct. Useful for declaring it is the record macro.


I expect that named tuples are usable as anonymous types or where structure is more important.

Reaching results of type {a : Int32, b : Int32} | {a : Int32, c : Int32} should be easier with named tuples rather than records. I am thinking of scenarios where you are merging named tuples, when puting a name to every type is not needed.

At the end it might be also used for named arguments probably. But it is fun to play around these things :-)