question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to support Mirrors for Generic Tuples arity +22

See original GitHub issue

Since #15250 we can now synthesize mirrors for generic tuple types if they have arity <= 22, we can do this because we pretend the mirror was for one of the equivalent scala.Tuple<N> classes.

We can’t do that for TupleXXL however as it has no accessor methods, and supporting them by creating fake accessor names would break the assumption that MirroredElemLabels corresponds to field accessors. Look at the example below:

Compiler version

3.2.0-RC1-bin-20220606-cec9aa3-NIGHTLY

Minimized example

case class Row(
  a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int,
  a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int,
  a21: Int, a22: Int, a23: Int
)

val mRow = summon[scala.deriving.Mirror.Of[Row]] // ok
val mElems = summon[scala.deriving.Mirror.Of[mRow.MirroredElemTypes]] // error

Output

-- Error: ----------------------------------------------------------------------
1 |summon[scala.deriving.Mirror.Of[mRow.MirroredElemTypes]] // error
  |                                                        ^
  |No given instance of type deriving.Mirror.Of[mRow.MirroredElemTypes] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[mRow.MirroredElemTypes]: 
  |	* class *: is not a generic product because it reduces to a tuple with arity 23, expected arity <= 22
  |	* class *: is not a generic sum because it does not have subclasses
1 error found

Expectation

we should be able to support generic tuples when arity is above 22. But the path to getting there would need some planning.

I propose that we could reinterpret MirroredElemLabels as a tuple of literal integer types for larger generic tuples, as there is currently no static constraint preventing this. e.g.

val res1: 
  scala.deriving.Mirror.Product{
    MirroredMonoType = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23);
    MirroredType = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23); 
    MirroredLabel = "Tuple23";
    MirroredElemTypes = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23);
    MirroredElemLabels = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22)
  } = anon$1@3f9e4811

Another alternative would be to add a new subtype of mirror: Mirror.Tuple, (probably also necessary to make explicit that the MirroredLabel no longer corresponds to a class name) and optionally a new Mirror.TupleOf[x *: y *: z *: EmptyTuple] summoner?

Either way the elem labels only act as unique identifiers, rather than having any runtime impact (and no you should not use the labels to access fields with java reflection, just use productIterator.)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
sjrdcommented, Jun 8, 2022

I believe the integers should start at 0, to match the values we can pass to the apply(i) method.

0reactions
nicolasstuckicommented, Jun 8, 2022

Probably a good idea to have them experimental to start with.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Variadic generics discussion - #60 by anandabits - Swift Forums
Is it even worth trying to resolve this mismatch, given that argument lists are intentionally not intended to mirror tuples? The impedance mismatch...
Read more >
Arity-Generic Datatype-Generic Programming
shed light on arity support in a generic programming frame- ... markably, but perhaps unsurprisingly, this notion of arity mirrors.
Read more >
Fundamentals of Database Systems
Part 9 includes Chapter 21 on transaction processing concepts; Chapter 22 ... Support material is available to all users of this book and...
Read more >
Announcing Dotty 0.18.1-RC1 – switch to the 2.13 standard ...
So far we do not plan to support something more complex than the above ... More Liftable instances – for Tuples of arity...
Read more >
XPath and XQuery Functions and Operators 3.1
At the time of writing, XSLT 3.0 requires support for XPath 3.0, ... where two functions have the same name and different arity, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found