# Named tuples: Bag-like operations

**URL:** <https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956>\
**Category:** Language Design\
**Created:** [December 12, 2024, 5:39am UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956 "2024-12-12T05:39:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![aboisvert](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/aboisvert/32/973_2.png) [@aboisvert](https://contributors.scala-lang.org/u/aboisvert)\
**Post date:** [December 12, 2024, 5:39am UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/1 "2024-12-12T05:39:15Z")

</div>

As an early adopter of named tuples, there are situations where I’d like to treat named tuples as a bag of named values rather than a strict sequence of named values.

For example, I’d love to be able to assign tuples that share the same names but in different order,

```scala
// could this be valid? (with the right operators or conversion in scope)
val bag: Bag[(foo: Int, bar: String)] = (bar = "hello", foo = 1)

```

And in other circumstances, I’d like to pass a set of named values and have the target type decide which ones it’s interested in:

```scala
// extract a subset of named values
val bag: Bag[(foo: Int, bar: String)] = (bar = "hello", baz = 1.0, foo = 42)

```

The resemblance to given’s is intended, in that order in unimportant and selection of available values (based on names instead of types) is desirable. And together, these would bring valuable modularity semantic.

I’m guessing libraries like Chimney could eventually do this, since they offer similar operations on case classes. At the same time, I’m wondering if these would be readily possible through some lightweight type-level programming constructs that may be better hosted in the standard library.

---

<div class="post-metadata">

**Author:** ![OndrejSpanel](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ondrejspanel/32/1238_2.png) [@OndrejSpanel](https://contributors.scala-lang.org/u/OndrejSpanel)\
**Post date:** [December 12, 2024, 7:11am UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/2 "2024-12-12T07:11:40Z")

</div>

> [@aboisvert](#):
>
> `val bag: Bag[(foo: Int, bar: String)] = (bar = "hello", foo = 1)`

You can already do:

```auto
val (foo = foo, bar = bar) = (bar = "hello", foo = 1)

```

But I guess you already know that anyway.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jasper-m/32/1192_2.png) [@Jasper-M](https://contributors.scala-lang.org/u/Jasper-M)\
**Post date:** [December 12, 2024, 11:00am UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/3 "2024-12-12T11:00:19Z")

</div>

It seems like what you really want is rather a subset of structural types. But I guess that’s what 99% of people who want named tuples _actually_ want.

---

<div class="post-metadata">

**Author:** ![spamegg1](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/spamegg1/32/2808_2.png) [@spamegg1](https://contributors.scala-lang.org/u/spamegg1)\
**Post date:** [December 12, 2024, 1:16pm UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/4 "2024-12-12T13:16:04Z")

</div>

The best approximation available is the [Selectable trait](https://docs.scala-lang.org/scala3/book/types-structural.html) right?

---

<div class="post-metadata">

**Author:** ![aboisvert](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/aboisvert/32/973_2.png) [@aboisvert](https://contributors.scala-lang.org/u/aboisvert)\
**Post date:** [December 12, 2024, 2:46pm UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/5 "2024-12-12T14:46:29Z")

</div>

Yes, I’m aware of `Selectable`, and it’s going in the right direction with the new `Fields` type member (mentioned in [Scala 3.6.2 is now available! | The Scala Programming Language](https://scala-lang.org/news/3.6.2)). But `Selectable` only helps with the method invocation part of the equation, so is insufficient.

What’s lacking is an algebra on named tuples that provides operations like union/intersection that works like you’d expect on named tuples. Case in point:

```auto
type T1 = (foo: Int, bar: String)
type T2 = (bar: String, foo: Int)
val t: T1 | T2 = ...
t.foo // Error: value 'foo' is not a member of T1 | T2

```

This is indeed where structural types could be helpful in some ways, but named tuples are (AFAIK) not expressible as structural types – at least not currently with type-level programming constructs, without resorting to macros. Maybe macros are the only solution here. I guess I’m back to my original question about whether we should expect this to be developed in the language, or in 3rd-party libraries.

---

<div class="post-metadata">

**Author:** ![bishabosha](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/bishabosha/32/3101_2.png) [@bishabosha](https://contributors.scala-lang.org/u/bishabosha)\
**Post date:** [December 12, 2024, 11:49pm UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/6 "2024-12-12T23:49:27Z")

</div>

I think the secret lies somewhere in this [Scala.IO](http://Scala.IO) 2024 talk from the author of the Ducktape library:  
[https://www.youtube.com/watch?v=I1\_dTu2A1r8](https://www.youtube.com/watch?v=I1_dTu2A1r8)

specifically a tutorial for how to build a “subfield” operation for named tuple types

---

<div class="post-metadata">

**Author:** ![som-snytt](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/som-snytt/32/595_2.png) [@som-snytt](https://contributors.scala-lang.org/u/som-snytt)\
**Post date:** [December 13, 2024, 7:28am UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/7 "2024-12-13T07:28:10Z")

</div>

The analogy with named args (to function applications) made me want relaxed ordering.

---

<div class="post-metadata">

**Author:** ![aboisvert](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/aboisvert/32/973_2.png) [@aboisvert](https://contributors.scala-lang.org/u/aboisvert)\
**Post date:** [December 13, 2024, 2:56pm UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/8 "2024-12-13T14:56:06Z")

</div>

> [@bishabosha](#):
>
> I think the secret lies somewhere in this [Scala.IO](http://Scala.IO) 2024 talk from the author of the Ducktape library […]

Thank you! Indeed this is very close to it. I’ll see if I can cook something up based on it.

---

<div class="post-metadata">

**Author:** ![MateuszKowalewski](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mateuszkowalewski/32/77_2.png) [@MateuszKowalewski](https://contributors.scala-lang.org/u/MateuszKowalewski)\
**Post date:** [December 13, 2024, 5:06pm UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/9 "2024-12-13T17:06:24Z")

</div>

> [@Jasper-M](#):
>
> It seems like what you really want is rather a subset of structural types. But I guess that’s what 99% of people who want named tuples _actually_ want.

In that case I’m the one percent man. 😄

I see named tuples as (anonymous) “structs”. Structs have named, _ordered_ fields.

“Unnamed” tuples have also a field order. So this is consistent.

If I wanted structural types I would use structural types… They exist already.

* * *

```scala
type T1 = (foo: Int, bar: String)
type T2 = (bar: String, foo: Int)
val t: T1 | T2 = ...
t.foo // Error: value 'foo' is not a member of T1 | T2

```

This OTOH is indeed unlucky.

But it’s consistent with the behavior of classes.

```scala
case class T1(foo: Int, bar: String)
case class T2(bar: String, foo: Int)
val t: T1 | T2 = ???
t.foo // Error: value foo is not a member of T1 | T2

```

That’s more a limitation of Scala’s current union / intersection types and less something named tuple specific.

I think such cases were actually discussed at length when unions / intersections were introduced. Scala just isn’t structurally typed and the current behavior is a consequence of that.

It would be of course nice if we got set operations on object fields, and could create ad-hoc new objects out of the results. But for that we would need structurally typed objects. Something like TS… Also this is not a new idea, but was also part of the discussions regarding `|` and `&` types. I’m not sure this will ever exist, though. Scala is nominally typed at the core. Everything else is glued on.

---

<div class="post-metadata">

**Author:** ![bishabosha](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/bishabosha/32/3101_2.png) [@bishabosha](https://contributors.scala-lang.org/u/bishabosha)\
**Post date:** [December 14, 2024, 5:05pm UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/10 "2024-12-14T17:05:29Z")

</div>

There are already fully supported structural types in scala 3, (i.e. `scala.Selectable`) the problematic part with those is they are harder to create programatically without dipping into macros

---

<div class="post-metadata">

**Author:** ![aboisvert](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/aboisvert/32/973_2.png) [@aboisvert](https://contributors.scala-lang.org/u/aboisvert)\
**Post date:** [December 15, 2024, 12:58am UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/11 "2024-12-15T00:58:03Z")

</div>

For those interested, I got my basic use-cases working:

- mapping to another named tuple with reordered fields
- mapping to another named tuple with a subset of fields

> **[GitHub - aboisvert/tuplelogy: Scala utility class for mapping named tuples](https://github.com/aboisvert/tuplelogy)**
>
> Scala utility class for mapping named tuples

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jasper-m/32/1192_2.png) [@Jasper-M](https://contributors.scala-lang.org/u/Jasper-M)\
**Post date:** [December 16, 2024, 10:22am UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/12 "2024-12-16T10:22:29Z")

</div>

> [@MateuszKowalewski](#):
>
> “Unnamed” tuples have also a field order. So this is consistent.

Yes but unnamed tuples need an order _because_ there are no names. Once your fields have names and you are using a higher-level language that can manage the low-level memory layout for you, there is no reason why you would care about the order of your fields.

Like som said:

> [@som-snytt](#):
>
> The analogy with named args (to function applications) made me want relaxed ordering.

In another thread someone made the analogy between named tuples and named arguments to explain the subtyping relationship. But the thing about named arguments is that once you use them the order in which you pass them does not matter anymore.

---

<div class="post-metadata">

**Author:** ![MateuszKowalewski](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mateuszkowalewski/32/77_2.png) [@MateuszKowalewski](https://contributors.scala-lang.org/u/MateuszKowalewski)\
**Post date:** [December 16, 2024, 8:11pm UTC](https://contributors.scala-lang.org/t/named-tuples-bag-like-operations/6956/13 "2024-12-16T20:11:12Z")

</div>

“Unordered tuples” wouldn’t be tuples any more. Such a thing is called “record”. But named tuples aren’t records. They are tuples, so they have field order.

Also, what would be the analog of “structs” in environments where memory layout _does mater_ if named tuples were records (which is indeed a higher level data structure than “structs”).

We currently don’t have records in Scala, but we have their types, namely structural types. There are also alternatives to records in Scala, like good old (case) classes. So not sure we need records. But in case, I guess one could build them with the help of that `tuplelogy` lib @aboisvert created, or likely in some other way. (There were a few proposals for records in the past.)

The point is, one needs first low level structures to build high level structures. A high level structure can’t replace a low level one. Things need to build on each other, from the basic things to the more complex.

That’s why I think named tuples should be seen as tuples, not as records. Tuples are the more basic structure and should come before records.
