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.

generic tuple not of subtype of ProductN

See original GitHub issue

Compiler version

3.1.2

Minimized code

case class Foo(a: Int, b: String)
val foo = Foo(1, "Hello")
var x: Tuple2[Int, String] = Tuple.fromProductTyped(foo)
var y: Product2[Int, String] = x
var z: Product2[Int, String] = Tuple.fromProductTyped(foo)

Output

-- [E007] Type Mismatch Error: -------------------------------------------------
5 |var z: Product2[Int, String] = Tuple.fromProductTyped(foo)
  |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                               Found:    (Int, String)
  |                               Required: Product2[Int, String]

Explanation
===========

Tree: Tuple.fromProductTyped[Foo](foo)(
  Foo.$asInstanceOf[
    
      (
        deriving.Mirror.Product{
          MirroredType = Foo; MirroredMonoType = Foo; MirroredElemTypes <: Tuple
        }
       & 
        scala.deriving.Mirror.Product{
          MirroredMonoType = Foo; MirroredType = Foo; 
            MirroredLabel = ("Foo" : String)
        }
      ){
        MirroredElemTypes = (Int, String); 
          MirroredElemLabels = (("a" : String), ("b" : String))
      }
    
  ]
)

I tried to show that
  (Int, String)
conforms to
  Product2[Int, String]
but the comparison trace ended with `false`:
          
  ==> (Int, String)  <:  Product2[Int, String]
  <== (Int, String)  <:  Product2[Int, String] = false

The tests were made under the empty constraint

Expectation

As (A, B) = Tuple2[A, B] is a direct subclass of Product2[Int, String], I would expect that the assignment to z compiles.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
SethTisuecommented, Sep 15, 2022

It’s really… shall we say, disquieting that the subtyping here isn’t transitive:

scala> summon[Int *: Int *: EmptyTuple <:< Tuple2[Int, Int]]
val res11: (Int, Int) =:= (Int, Int) = generalized constraint
                                                                                                    
scala> summon[Tuple2[Int, Int] <:< Product2[Int, Int]]
val res12: (Int, Int) =:= (Int, Int) = generalized constraint
                                                                                                    
scala> summon[Int *: Int *: EmptyTuple <:< Product2[Int, Int]]
-- Error: ----------------------------------------------------------------------
1 |summon[Int *: Int *: EmptyTuple <:< Product2[Int, Int]]
  |                                                       ^
  |                      Cannot prove that (Int, Int) <:< Product2[Int, Int].
1 error found

🙀

It’s rather cold comfort to know that Tuple2 might eventually go away in some possible future.

0reactions
maxm123commented, May 24, 2022

This is consistent with the current design as Tuple2[A, B] <:< (A, B) and not equal.

But why does the assignment to x work, then? If fromProductTyped returns (Int, String) which is not <:< Tuple2[…], the variable type should mismatch.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mapped array/tuple types can't be used as generic ... - GitHub
There should be no error, as mapped types do not map over all keys in the array/tuple, it should only consider Type[K] to...
Read more >
Scala generic (tuple) type with multiple subtypes
The first solution is to use Product , as said by @om-nom-nom. Indeed, it's the only common supertype of all tuples.
Read more >
Tuples bring generic programming to Scala 3
Tuples allow developers to create new types by associating existing types. In doing so, they are very similar to case classes but unlike...
Read more >
5. Introduction to Types and Generic Programming
In this case, Tuple is the parametric type, and the three parameters are a list of the types of each value. The parametric...
Read more >
PEP 646 – Variadic Generics - Python Enhancement Proposals
From the signature alone, it is not obvious what shape of array [2] we should pass for the ... Using Type Variable Tuples...
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