我被告知 Scala 3 元组的行为类似于 Scala 2 中的无形状 HList,所以我做了一个简单的实验:
val tuple = (1, "String", 1.1)
val _: *:[Int, *:[String, Double]] = tuple // not working
val _: *:[Double, *:[String, Int]] = tuple // not working
其中任何一个都应该基于 HList 定义工作,这是怎么发生的?
完整错误:
[Error] /home/peng/git/dottyspike/src/main/scala/com/tribbloids/spike/dotty/NewTuple.scala:15:42: Found: (tuple : (Int, String, Double))
Required: Int *: String *: Double
Explanation
===========
Tree: tuple
I tried to show that
(tuple : (Int, String, Double))
conforms to
Int *: String *: Double
but none of the attempts shown below succeeded:
==> (tuple : (Int, String, Double)) <: Int *: String *: Double class dotty.tools.dotc.core.Types$CachedTermRef class dotty.tools.dotc.core.Types$CachedAppliedType
==> (Int, String, Double) <: Int *: String *: Double class dotty.tools.dotc.core.Types$CachedAppliedType class dotty.tools.dotc.core.Types$CachedAppliedType
==> (String, Double) <: String *: Double class dotty.tools.dotc.core.Types$CachedAppliedType class dotty.tools.dotc.core.Types$CachedAppliedType
==> Double *: EmptyTuple.type <: Double class dotty.tools.dotc.core.Types$CachedAppliedType class dotty.tools.dotc.core.Types$CachedTypeRef = false
The tests were made under the empty constraint
[Error] /home/peng/git/dottyspike/src/main/scala/com/tribbloids/spike/dotty/NewTuple.scala:17:42: Found: (tuple : (Int, String, Double))
Required: Double *: String *: Int
Explanation
===========
Tree: tuple
I tried to show that
(tuple : (Int, String, Double))
conforms to
Double *: String *: Int
but none of the attempts shown below succeeded:
==> (tuple : (Int, String, Double)) <: Double *: String *: Int class dotty.tools.dotc.core.Types$CachedTermRef class dotty.tools.dotc.core.Types$CachedAppliedType
==> (Int, String, Double) <: Double *: String *: Int class dotty.tools.dotc.core.Types$CachedAppliedType class dotty.tools.dotc.core.Types$CachedAppliedType
==> Int <: Double class dotty.tools.dotc.core.Types$CachedTypeRef class dotty.tools.dotc.core.Types$CachedTypeRef = false
The tests were made under the empty constraint
two errors found
找到正确的定义:
val _: Int *: String *: Double *: EmptyTuple = tuple