# Old style tuple accessors on normal case classes in Scala 3

**URL:** <https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394>\
**Category:** Question\
**Created:** [March 26, 2022, 2:49pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394 "2022-03-26T14:49:15Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![devlaam](https://users.scala-lang.org/user_avatar/users.scala-lang.org/devlaam/32/2246_2.png) [@devlaam](https://users.scala-lang.org/u/devlaam)\
**Post date:** [March 26, 2022, 2:49pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/1 "2022-03-26T14:49:15Z")

</div>

The following code compiles on [Scala 3.1.1](https://scastie.scala-lang.org/P9m36ankSIOHnD8y6CEEGg) but not on [Scala 2.13.8](https://scastie.scala-lang.org/bJY5muBVRIavbhuV2fmvXA):

```scala
case class Text(number: Int, line: String)

object Main {
  val a = Text(42,"The answer") 
 
  def main(args: Array[String]): Unit = 
  { println(s"The number is '${a._1}'") 
    println(s"The line is '${a._2}'") } }

```

is this intentional or a bug? Btw, for normal classes it does not compile on [Scala 3.1.1](https://scastie.scala-lang.org/ZhMIXmpTSZmY6nbpN8MELQ)

---

<div class="post-metadata">

**Author:** ![chaotic3quilibrium](https://users.scala-lang.org/user_avatar/users.scala-lang.org/chaotic3quilibrium/32/1745_2.png) [@chaotic3quilibrium](https://users.scala-lang.org/u/chaotic3quilibrium)\
**Post date:** [March 26, 2022, 5:54pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/2 "2022-03-26T17:54:22Z")

</div>

Hmm. I am confused. You have an instance of `Text` referenced by `a`. The only way to access those values is via their names. As in `a.number` and `a.line`.

And as you said, this works in 3.1, but not in 2.13.

[Here is the Scastie modifying your code to work in 2.13](https://scastie.scala-lang.org/KQSg3DAUQh6f3qmRUuQUBA).

[Here is the Scastie with exactly the same 2.13 modified code, but just set the compiler to 3.1](https://scastie.scala-lang.org/1DAuGMMLTcaqawbfNqgHhA), and it failed on `val a = Text.unapply(text).get` with an error message of `value get is not a member of Playground.Text`.

Unless something drastically changed in the transition to Scala 3.0, I don’t know why the code is failing.

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [March 26, 2022, 10:02pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/3 "2022-03-26T22:02:13Z")

</div>

Fascinating. The question, mind, is why this is working at all in Scala 3. I mean, there’s no reason why it _should_ work, and I’m totally unsurprised that it fails in Scala 2. So this has to be a side-effect of some enhancement in Scala 3.

I would _guess_ that it is coming out of the [parameter untupling changes](https://docs.scala-lang.org/scala3/reference/other-new-features/parameter-untupling-spec.html), but that’s just a guess based on two minutes of research, and I’m not at all sure that it’s plausible.

(My first guess had been that in Scala 3, your case class was now extending `Product2` rather than just `Product` as in Scala 2; however, [a quick experiment doesn’t seem to support that](https://scastie.scala-lang.org/b13LIL8mTEqsYotCKyGXlA).)

---

<div class="post-metadata">

**Author:** ![smarter](https://users.scala-lang.org/user_avatar/users.scala-lang.org/smarter/32/262_2.png) [@smarter](https://users.scala-lang.org/u/smarter)\
**Post date:** [March 26, 2022, 10:37pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/4 "2022-03-26T22:37:06Z")

</div>

This is intentional. Case classes in Scala 3 define \_N fields, combined with an unapply method that returns the case class instance itself this allows for [pattern matching without unnecessary allocations](http://dotty.epfl.ch/docs/reference/changed-features/pattern-matching.html) (Scala 2 was also capable of matching on case classes without allocating, but this was done by special-casing case classes, whereas in Scala 3 it’s just a consequence of how they’re desugared)

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [March 26, 2022, 10:58pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/5 "2022-03-26T22:58:46Z")

</div>

Neat – I was wondering if it might be something of the sort. Is this enhancement spelled out anywhere in the Dotty docs? It’s a nice little detail, and seems to be pretty obscure currently…

---

<div class="post-metadata">

**Author:** ![smarter](https://users.scala-lang.org/user_avatar/users.scala-lang.org/smarter/32/262_2.png) [@smarter](https://users.scala-lang.org/u/smarter)\
**Post date:** [March 26, 2022, 11:02pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/6 "2022-03-26T23:02:19Z")

</div>

No, it doesn’t seem to be documented anywhere.

---

<div class="post-metadata">

**Author:** ![chaotic3quilibrium](https://users.scala-lang.org/user_avatar/users.scala-lang.org/chaotic3quilibrium/32/1745_2.png) [@chaotic3quilibrium](https://users.scala-lang.org/u/chaotic3quilibrium)\
**Post date:** [March 27, 2022, 2:54am UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/7 "2022-03-27T02:54:45Z")

</div>

So, shouldn’t this be identified and documented in the “Transition from 2.x to 3.x Document” (cannot remember its official name) which explains all the differences?

And I find it fascinating there was no code in the huge testing corpus that didn’t use `unapply`. And if there is, then why didn’t those tests fail?!

---

<div class="post-metadata">

**Author:** ![chaotic3quilibrium](https://users.scala-lang.org/user_avatar/users.scala-lang.org/chaotic3quilibrium/32/1745_2.png) [@chaotic3quilibrium](https://users.scala-lang.org/u/chaotic3quilibrium)\
**Post date:** [March 27, 2022, 2:57am UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/8 "2022-03-27T02:57:39Z")

</div>

I think [this is where](https://docs.scala-lang.org/scala3/guides/migration/compatibility-intro.html) all the “transition from Scala 2 to Scala 3” notes are being collected and organized.

---

<div class="post-metadata">

**Author:** ![devlaam](https://users.scala-lang.org/user_avatar/users.scala-lang.org/devlaam/32/2246_2.png) [@devlaam](https://users.scala-lang.org/u/devlaam)\
**Post date:** [March 27, 2022, 6:32am UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/9 "2022-03-27T06:32:54Z")

</div>

This was a helpful discussion, thanks for all the answers. I was suspecting it could be intentional, but was unsure, because i thought i read somewhere that the old style tuple accessors were later phased out. But now i assume they are not.

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [March 27, 2022, 4:30pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/10 "2022-03-27T16:30:14Z")

</div>

I think we’re talking about two different things here – I’m referring to the new built-in `._1`, `._2` feature, which isn’t documented yet. The rewrites to unapply were extensively documented, [as @smarter linked to above](http://dotty.epfl.ch/docs/reference/changed-features/pattern-matching.html).

As to the change: honestly, the pattern you show is kind of obscure – I didn’t even know it could be done that way in Scala 2. (I don’t recall seeing it documented anywhere.) And there are better ways to accomplish the same goal in Scala 3. So yes, it’s a language change, but seems like one of the more minor ones…

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [March 27, 2022, 4:32pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/11 "2022-03-27T16:32:14Z")

</div>

> [@devlaam](#):
>
> because i thought i read somewhere that the old style tuple accessors were later phased out. But now i assume they are not.

I think it’s the other way around – this never worked for case classes before, but now it does. (That is, there is no “old style” about it.) Do you have reason to believe that tuple accessors worked here before Scala 3?

---

<div class="post-metadata">

**Author:** ![chaotic3quilibrium](https://users.scala-lang.org/user_avatar/users.scala-lang.org/chaotic3quilibrium/32/1745_2.png) [@chaotic3quilibrium](https://users.scala-lang.org/u/chaotic3quilibrium)\
**Post date:** [March 27, 2022, 5:05pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/12 "2022-03-27T17:05:33Z")

</div>

I am very happy to see the `._1` and `._2` feature was added. I hope it gets documented somewhere soon. I would have happily used the older pattern when transitioning my Scala 2 codebase, and been quite frustrated hitting this particular issue.

And I am honestly very glad to hear it is obscure. It wasn’t and isn’t for me and my codebase(s).

As such, it would still be nice to have this pattern “called out” as a migration issue guiding me to the meta and then the proper way to approach it in Scala 3.

These kinds of little edge cases can become a blocker when someone is trying to (quickly) move a codebase from Scala 2 to Scala 3.

---

<div class="post-metadata">

**Author:** ![devlaam](https://users.scala-lang.org/user_avatar/users.scala-lang.org/devlaam/32/2246_2.png) [@devlaam](https://users.scala-lang.org/u/devlaam)\
**Post date:** [March 27, 2022, 9:00pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/13 "2022-03-27T21:00:54Z")

</div>

> [@jducoeur](#):
>
> I think it’s the other way around – this never worked for case classes before, but now it does.

Indeed, and this observation was exactly the reason I made de post. To find out which of the ones below holds true:

1. Me being stupid
2. New undocumented behaviour
3. A bug in Scala 2: it should have always worked, but was never noticed.
4. A bug in Scala 3: it should not work, but suddenly it does.

The second turns out to be the case.

> [@jducoeur](#):
>
> (That is, there is no “old style” about it.)

Calling this “old style” is based on footnote 6 on page 83 of " Programming in Scala, Fifth Edition" stating “Note, prior to Scala 3, you accessed the elements of a tuple using one-based field names, such as \_1 or \_2.”

> [@jducoeur](#):
>
> Do you have reason to believe that tuple accessors worked here before Scala 3?

No, i did not. I knew it failed for Scala 2 so that is why i was surprised it suddenly did. (I noticed it while refactoring a tuple to a case class)

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [March 27, 2022, 10:06pm UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/14 "2022-03-27T22:06:13Z")

</div>

> [@devlaam](#):
>
> Calling this “old style” is based on footnote 6 on page 83 of " Programming in Scala, Fifth Edition" stating “Note, prior to Scala 3, you accessed the elements of a tuple using one-based field names, such as \_1 or \_2.”

… huh, that’s interesting phrasing, and I don’t think of it as having changed in normal cases. Is it referring to the fact that you can now do other things with tuples of more than 22 elements, or has something else changed?

---

<div class="post-metadata">

**Author:** ![mpilquist](https://users.scala-lang.org/user_avatar/users.scala-lang.org/mpilquist/32/1958_2.png) [@mpilquist](https://users.scala-lang.org/u/mpilquist)\
**Post date:** [March 28, 2022, 12:03am UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/15 "2022-03-28T00:03:30Z")

</div>

> [@jducoeur](#):
>
> … huh, that’s interesting phrasing, and I don’t think of it as having changed in normal cases. Is it referring to the fact that you can now do other things with tuples of more than 22 elements, or has something else changed?

I believe that note is in contrast to Scala 3’s 0-based integer indexing. For example:

```scala
scala> val t = ("Hello", 42, true)
val t: (String, Int, Boolean) = (Hello,42,true)

scala> t(0)
val res0: String = Hello

scala> t(1)
val res1: Int = 42

scala> t(2)
val res2: Boolean = true

scala> t(3)
-- Error: ----------------------------------------------------------------------
1 |t(3)
  | ^
  | Match type reduction failed since selector EmptyTuple.type
  | matches none of the cases
  |
  | case x *: xs => (0 : Int) match {
  | case (0 : Int) => x
  | case scala.compiletime.ops.int.S[n1] => scala.Tuple.Elem[xs, n1]
  | }
1 error found

```

---

<div class="post-metadata">

**Author:** ![OndrejSpanel](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ondrejspanel/32/2318_2.png) [@OndrejSpanel](https://users.scala-lang.org/u/OndrejSpanel)\
**Post date:** [September 17, 2024, 10:37am UTC](https://users.scala-lang.org/t/old-style-tuple-accessors-on-normal-case-classes-in-scala-3/8394/16 "2024-09-17T10:37:24Z")

</div>

More than two years later the feature is still basically undocumented, at least I did not find anything in spite of searching.
