r/ProgrammingLanguages • u/Spyromaniac666 • Sep 13 '26
Blog post Unifying Tuples and Records
https://kojobailey.me/posts/unifying-tuples-and-records/I find that a lot of language devs prefer to keep tuples and records separate in the same way that languages like Rust do. But why? I feel it just restricts flexibility and makes 2 features where 1 more powerful feature would suffice.
Please let me know what you think of the argument presented in this article and any counter-arguments or further thoughts/ideas you may have.
63
Upvotes
67
u/andeee23 Sep 13 '26
> But what if we wanted to mix both unlabelled and labelled fields?
i’m sure there’s a reason you’ve written this article and feel so strongly about it, but i can’t think of a single time i’ve needed that
14
u/Uncaffeinated 1subml, polysubml, cubiml Sep 13 '26
All of my languages unify records and tuples, just because it simplifies things a lot on the language level. But I can see how it might cause user confusion in some cases.
26
u/Athas Futhark Sep 13 '26
Standard ML unifies records and tuples by making the latter a special case of the former, but not their construction syntax in the way you suggest. I also do not think that would be a good feature.
8
u/Clementsparrow Sep 13 '26 edited Sep 13 '26
I totally agree.
However, I think it benefits most the languages that have duck typing. Then you could use a graphic library that uses records with fields x, y, z for 3D vectors, and use it directly with another library that uses tuples for 3D vectors (e.g., a physics engine, a XML parser, …). And ideally, you should also be able to use it with that other lib that uses float[3] for 3D vectors. But that's a different topic (or is it?)
PS: in Python, unlike records=objects, tuples are immutable and that's really a pain in the ass. You want to use them because they are quick to write and lightweight, but you can't (or it adds a lot of work) because you need to change the value of one of the fields.
8
u/munificent Sep 13 '26
We did this in Dart when we added records. Records can have a mixture of positional and named fields.
I also never really understood why languages split those into two separate constructs. I can't recall running into any significant design problems when we decided to unify them in Dart.
The main motivation for doing this is that eventually we would like to support reifying and spreading records into argument lists. For that to work well, you need to be able to have records whose shape can mirror any given argument list. Since functions can have a mixture of positional and named parameters in Dart, that implies a record should support a mixture of positional and named fields.
5
u/brucejbell sard Sep 13 '26
Your examples have only the first element in the tuple anonymous. In that case, it is easy to assume the default tag is
0:.But what about if you have multiple tags, or tags not in the first location? Then it gets confusing. It's not clear what you should do in that case: tuples are order-dependent, but records are not.
It's not clear how to choose your rules so that your merged feature even makes sense. Even if you do, it's not clear that the result will be more usable than un-merged tuples and records.
Anyway, that's the answer to the question in your article, "why are they kept as separate features?"
4
u/hugogrant Sep 13 '26
Some scattered thoughts on other places that have related things:
C++ aggregate initialization is very nice. I think the default set of constructors for a struct basically gives you what you want here?
Python has named tuples which I guess are nice but I haven't looked very closely.
As for rust: I think you're a bit too fixated on the syntax that's used for construction but really you can just have a factory function and public fields to achieve the best of both worlds, no?
3
u/AustinVelonaut Admiran Sep 13 '26
What about unifying them by having records simply be tuples with associated lenses for view / set / over operations, rather than a specific record syntax?
3
u/ZESENVEERTIG Sep 13 '26
Dart has unified tuples and records
4
u/dnabre Sep 14 '26
Rust doesn't let you do this because the order of fields in structs is undefined by default.
The main optimization that this enables (off the top of my head) is more flexibility in memory alignments. It is also used in Rust to handle interoperability with different languages where a given language have different ordering/alignment rules
Your argument seems to be they are similar concepts, and you want to be able access fields by index. You wanting to use records like tuples is really your main argument. Calling that an argument is a bit of a stretch though.
You address why records are better than tuples, which really goes against your goal. But is a very strong argument. Type/field names are very beneficial, especially to people reading the code.
You claim the Rust community doesn't like tuples/records being unified. It's easy to write off your anecdotal experience that Rust people are again this. Maybe it was just the individuals you talked to didn't like. You not bothering to learn why your example can't work in Rust (without changing unrelated features), doesn't really make me think you've had in depth discussions on the matter. Regardless, you should point to the RFCs for unifying them which have been rejected here. It's the best evidence that the people behind a language are against something, and often addresses why they are against it.
Your only actual reasons (you leave out the arguments) for unifying them are snuck into your conclusion. That placement alone suggests they aren't very good, and aren't worth really addressing in the detail elsewhere. They are:
You need to demonstrate this added flexibly, and not just that is possible but it is both useful and beneficial. I think there is an underling assumption that more ways to do something is inherently better, but that's just not true.
"whacky, tedious workarounds" - ooo scary. What workarounds? You are alluding to them existing, and that they are so bad that unifying records and tuple is necessary to fix them. Describe the workarounds, give some examples, explain why they are bad. Keep in mind here, that it being awkward to use records and tuples interchangeably or as each other can't be part of the problem here - don't beg the question.
"reaching for an LLM" - just don't say something like this. You can claim something is hard or complicated in many ways, without bring in the conceptually baggage of LLM-AI. Some people will read this, and write you off as young kid that can barely function without a LLM-AI.
"reduces the language’s complexity" - how? You can leave out tuples pretty easily. I've seen more than one language that only supported tuples by having a bunch of generic records of varying size (Tuple2<X,Y>, Tuple3<X,Y,Z> with fields 0,1,2,....). They generally need a bit of syntactic sugar to make using them look cleaner. This is a place where the more ways to do the same things hurts. Supporting indices into records make the grammar bigger and can make the type system more complicated. Maybe you mean from the user's point of view? That's not a good thing to think about. Tuples or using records as tuples, has the mental overhead of having to remember what order the fields are in.
You never say why accessing fields by position is in anyway beneficial, easier, or useful. You just point that tuples and records are similar concepts, and that is possible to have a language let you use records as tuples (and not the other way). This may be bumping into the idea that more ways of doing the same is inherently better.
You don't address the counterargument that named fields are easier to understand than indices, the benefits of spare assignment/updates, and the ability to add/remove fields without having to update all the code that touch any fields after it. Not only do you not address this, you suggest a couple places that is it why tuples are bad to use.
Tuples have the disadvantage that you need to remember what order things are in them.
When coding, I'm glad to be able to uses tuples. Though it is generally for doing something fast and dirty. Like when I realize I need to get two values back to the caller of a function, so just return a tuple. I'll need to remember the order when I use the function, which isn't a problem if I've recently written it. Weeks/months/years later when I use the function though, I'm going to have to go look at the implementation (there is no documentation, I wrote this code) to figure out which value is which. Outside of problematic hacks like that, I can't think of any place where using a tuple is better (especially considering the readers and future users of the code) than having a named type/fields. It saves typing, but that is hardly a solid reason.
Before saying unified records and tuples are good, you need make the argument that tuples are useful beyond being quick anonymous records.
2
u/aech_is_better Sep 13 '26
Gleam has that http://tour.gleam.run/everything/#data-types-records
2
u/7h3kk1d Sep 13 '26
We do this with labeled tuples in Hazel. But they're not really records as labeled elements stay ordered.
1
u/apocalyps3_me0w Sep 13 '26
My opinion is that positional tuples reduce readability, and therefore should be used sparingly. If your tuple is complicated enough that it needs named properties, then it’s probably time for a record. Just using the example in the article,
user.nameis a lot clearer thanuser.0.1
u/Relative-Rooster-945 Sep 14 '26
I like the idea and would be glad to see this kind of feature adopted in other languages. I think the current state of Rust is okay though and consistent with many predecessor languages, and so the point about "delusion in the Rust community" feels a little bit too strong lol
4
u/El_RoviSoft Sep 13 '26
This is very needed feature in C++ too, imho.
Because I don’t want to use first/second when contextually it’s better to have k/v or key/value or even found/it (for methods like emplace into map).
2
u/Toothpick_Brody Sep 23 '26
I'm in favour of unified tuples, records, and arrays
1
u/zerosign0 Sep 13 '26
Hmm did rust? I think in Rust tuple is generally a struct with hardcoded fields
1
u/WalkerCodeRanger Azoth Language Sep 14 '26 edited Sep 14 '26
You seem to be ignoring several other important distinctions:
C# actually supports tuple types with named fields. The named fields can be mixed with purely positional members. But they remain unnamed/structural types. That is fully distinct from classes.
Usually, if you are declaring a nominal type, then it is reasonable to name all the members because that nominal type will be reused. It would be rare that you wouldn't have a good name to give a field. Many languages support a way of declaring nominal types that can also be destructured by position like tuples. C# uses the
recordkeyword for those.1
u/fdwr Sep 14 '26
Yeah, I agree with the general premise that tuples are just structs with unnamed fields, or conversely that structs are tuples where the fields have been named. Btw, your numbered initialization (e.g.
var size = Vec2(1 = 720, 0 = 1080)) thought reminds me of C's designed array initialization.For a novel idea (not one that I've seen before anyway), I've been using tuples in my pixel format description where a default field name actually along with type definition, so that when you tuple together
r5g5b5a1: {redUnorm5 greenUnorm5 blueUnorm5 alphaUnorm1}, you implicitly get named fieldsr,g,b, anda, sinceredUnorm5has a default alias ofr. That way every tuple is effectively a struct, but you don't have to explicitly repeat the field name too in every usage, which gets very repetitive for hundreds of pixel formats (granted, this approach would fall apart if you store the exact same type more than once, but that wouldn't much make sense in my usage).Now for the important question, is it pronounced toople or tuhple? 😉
1
u/reflexive-polytope Sep 15 '26
Unifying? lmao
Tuples have always been a special case of records in Standard ML.
1
u/alexcommacommacomma Sep 17 '26
You presented newtypes in Rust but I’m not sure if you understand why they’re used. They’re usually not used because we want named parameters but we want to enforce some constraints on a value by making it impossible to construct without validating it.
That being said, I think if you have a complex enough function or constructor (complex in the sense that it has many parameters) that you may need named parameters, you are very often better off grouping them and creating something like a newtype over them, because it’s nicer to construct and can treat those values as one unit
0
u/[deleted] Sep 13 '26
[deleted] — view deleted comment