# Named tuples are not considered to be disjoint from any type

**URL:** <https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070>\
**Category:** Language Design\
**Created:** [March 14, 2025, 8:38am UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070 "2025-03-14T08:38:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [March 14, 2025, 8:38am UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/1 "2025-03-14T08:38:46Z")

</div>

Due to the implementation of `NamedTuple` as opaque type with an upper-bound of `Any`, they are not considered to be disjoint from any type. This significantly limits their usage in match types:  
[scastie](https://scastie.scala-lang.org/TommJznhTWOg2Ll21hgR1A)

```scala
sealed trait Foo

type Bar[T] = T match
  case Foo => (a: Int, b: Int)
  case _ => NamedTuple.From[T]

val x: Bar[Foo] = ???
val xCheck = x.a //ok

val y: Bar[(1, 2)] = ???
val yCheck = y._1 //ok

val z: Bar[(a: Int, b: Int)] = ???
val zCheck = z.a //error value a is not a member of Bar[(a : Int, b : Int)]

```

I think we should special-case `NamedTuple` to be considered as `Tuple` in match types, so that its usability won’t be hindered compared to `Tuple`.

cc @sjrd

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [March 14, 2025, 8:59am UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/2 "2025-03-14T08:59:46Z")

</div>

Related ticket

> <https://github.com/scala/scala3/issues/22422>
>
> Assume you have a type level algorithm over named tuples that needs to recurse w…hen the value type is a named tuple, otherwise do something else. (e.g. check a named tuple is substructually equivalent to another)
> 
> Currently there isn't a way to do that because you cannot differentiate named tuple from other types
> 
> \## Compiler version
> 
> 3.6.3
> 
> \## Minimized code
> 
> \<!--
> This code should be self contained, compilable (with possible failures) and as small as possible.
> 
> Ideally, we should be able to just copy this code in a file and run \`scalac\` (and maybe \`scala\`) to reproduce the issue.
> \--\>
> 
> Perhaps there is a better way, but for simplicity you can not use match types on their own because the upper bound of AnyNamedTuple is Any.
> 
> \`\`\`Scala
> type IsNamedTuple\[T\] = T match
> case AnyNamedTuple =\> false
> case \_ =\> true
> 
> val x: IsNamedTuple\[NamedTuple.Empty\] = true
> val y: IsNamedTuple\[(foo: Int)\] = true
> val z: IsNamedTuple\[Int\] = false // error
> val q: IsNamedTuple\[String\] = false // error
> \`\`\`
> 
> \## Output
> 
> \`\`\`
> val z: IsNamedTuple\[Int\] = false // error
> Found: (false : Boolean)
> Required: IsNamedTuple\[Int\]
> 
> Note: a match type could not be fully reduced:
> 
> trying to reduce IsNamedTuple\[Int\]
> failed since selector Int
> does not match case NamedTuple.AnyNamedTuple =\> (true : Boolean)
> and cannot be shown to be disjoint from it either.
> Therefore, reduction cannot advance to the remaining case
> 
> case \_ =\> (false : Boolean)
> \`\`\`
> 
> \`\`\`
> val q: IsNamedTuple\[String\] = false // error
> Found: (false : Boolean)
> Required: IsNamedTuple\[String\]
> 
> Note: a match type could not be fully reduced:
> 
> trying to reduce IsNamedTuple\[String\]
> failed since selector String
> does not match case NamedTuple.AnyNamedTuple =\> (true : Boolean)
> and cannot be shown to be disjoint from it either.
> Therefore, reduction cannot advance to the remaining case
> 
> case \_ =\> (false : Boolean)
> \`\`\`
> 
> \## Expectation
> 
> the standard library could have a way to determine if a type is a match type or not, perhaps a primitive compile-time op?
> 
> \`\`\`scala
> object NamedTuple:
> type IsNamedTuple\[T\] \<: Boolean
> \`\`\`
> 
> Another trick I wanted to try was if \`NamedTuple.From\[T\]\` would reduce or not, but again this isnt useful because \`NamedTuple.From\[(foo: Int)\]\` matches \`NamedTuple.From\[t\]\` and also \`NamedTuple\[ns, vs\]\` so cant be distinguished from a reduced or non-reduced form

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [March 14, 2025, 9:17am UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/3 "2025-03-14T09:17:20Z")

</div>

Named tuples are not _implemented_ as opaque type aliases. They are _specced_ as opaque type aliases. Under that constraint, they must behave the way they do with match types.

If you want something else, you must argue that named tuples should be specced as (magic) classes, not opaque type aliases.

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [March 14, 2025, 9:41am UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/4 "2025-03-14T09:41:34Z")

</div>

> [@sjrd](#):
>
> If you want something else, you must argue that named tuples should be specced as (magic) classes, not opaque type aliases.

Why can’t the match type spec special-case an opaque type of `NamedTuple`?

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [March 14, 2025, 10:02am UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/5 "2025-03-14T10:02:39Z")

</div>

Because match types are hard and subtle enough as it is. It’s not the match types’ responsibility to fix up things that could have been specced differently elsewhere.

---

<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:** [March 18, 2025, 12:51pm UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/6 "2025-03-18T12:51:52Z")

</div>

how would you change that spec - could the library stay the same?

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [March 18, 2025, 1:07pm UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/7 "2025-03-18T13:07:33Z")

</div>

When a library relies on the implementation of named tuples (and most will probably will) you will need to break source backwards compatibility.

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [March 18, 2025, 1:12pm UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/8 "2025-03-18T13:12:12Z")

</div>

No it would totally and utterly break compatibility, in all categories except perhaps binary.

---

<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:** [March 18, 2025, 7:15pm UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/9 "2025-03-18T19:15:38Z")

</div>

inline match however is able to make the distinction - but you would have to resort to context parameters and type members

```auto
sealed trait Foo

trait Bar0[+T] { type Res; def res: Res }

object Bar0:
  type Aux[T, R] = Bar0[T] { type Res = R }
  transparent inline given mkBar[T]: Bar0[T] =
    inline compiletime.erasedValue[T] match
      case _: Foo =>
        new Bar0[Foo] { type Res = (a: Int, b: Int); def res: Res = ??? }
          .asInstanceOf[Bar0.Aux[T, (a: Int, b: Int)]]
      case _ =>
        new Bar0[T] { type Res = NamedTuple.From[T]; def res: Res = ??? }
          .asInstanceOf[Bar0.Aux[T, NamedTuple.From[T]]]

val x = summon[Bar0[Foo]]
val xCheck = x.res.a

val y = summon[Bar0[(1, 2)]]
val yCheck = y.res._1

val z = summon[Bar0[(a: Int, b: Int)]]
val zCheck = z.res.a

```

---

<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:** [March 18, 2025, 7:43pm UTC](https://contributors.scala-lang.org/t/named-tuples-are-not-considered-to-be-disjoint-from-any-type/7070/10 "2025-03-18T19:43:54Z")

</div>

if you want a DSL:

```auto
type Bar = TypeFun[(Foo ->> (a: Int, b: Int), TypeFun.Wild[[T] =>> NamedTuple.From[T]])]

val x0 = summon[Bar[Foo]]
val x0Check: x0.Out = (a = 1, b = 2)

val y0 = summon[Bar[(1, 2)]]
val y0Check: y0.Out = (1, 2)

val z0 = summon[Bar[(a: Int, b: Int)]]
val z0Check: z0.Out = (a = 1, b = 2)

```

based on:

```auto
type TypeFun[Cases <: Tuple] = [T] =>> TypeFun.Resolved[T, Cases]
type ->>[Case, R] = (Case, R)

object TypeFun:
  sealed trait Wild[F[_]]
  final class Resolved[T, Cases <: Tuple]:
    type Out

  object Resolved:
    type Aux[T, Cases <: Tuple, R] = Resolved[T, Cases] { type Out = R }
    transparent inline given resolved[T, Cases <: Tuple]: Resolved[T, Cases] =
      resolveCases[T, Cases, Cases]

    transparent inline def resolveCases[T, Cases <: Tuple, C <: Tuple]: Resolved[T, Cases] =
      inline compiletime.erasedValue[C] match
        case _: ((T, r) *: _) => Resolved[T, Cases].asInstanceOf[Resolved.Aux[T, Cases, r]]
        case _: (Wild[f] *: _) => Resolved[T, Cases].asInstanceOf[Resolved.Aux[T, Cases, f[T]]]
        case _: (_ *: rest) => resolveCases[T, Cases, rest]

```

maybe someone could make it work with more kinds of “patterns”
