generic tuple not of subtype of ProductN
See original GitHub issueCompiler 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:
- Created a year ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s really… shall we say, disquieting that the subtyping here isn’t transitive:
🙀
It’s rather cold comfort to know that
Tuple2
might eventually go away in some possible future.But why does the assignment to x work, then? If fromProductTyped returns (Int, String) which is not <:< Tuple2[…], the variable type should mismatch.