Skip to content

Type inference on type lambdas w.r.t tuples is confusing #3075

Closed
@raymondtay

Description

@raymondtay

I was reading over dotty's new type lambda syntax and exploring its feature when i came across an observation when i declared the following expressions in dotty's REPL (Using Dotty 0.3-RC1):

scala> type T = [X, _] => (X,X)
scala> val t : T[Int, _] = (1,2)
val t : Int, Int = (1,2) // the wildcard "inherited the 'Int' type"

I guess my question is whether this is legal because in Scala, it seems to interpret to Nothing.

Another example is the following using dotty's REPL

scala> type T = [X, _ , _] => (X, X)
scala> val t: T[Int, _, _] = (1,2)
val t : Int, Int = (1,2) // the 3rd wildcard is gone !

Last example w.r.t type lambdas in tuples is what seems like the usual arithmetic promotion rules in Java/Scala:

scala> type T = [X,_, _] => (X,X)
defined type alias T
scala> val t : T[Float,_ ,_] = (1f, 333)
val t: Float, Float = (1.0,333.0) 
//
// Here's the weird part
//
scala> val a : (Float, _) = (1f, 222)
val a: Float, _ = (1.0,222)
scala> a._2
val res2: a.T2 = 222 // <---- a.T2 is a type that has not been resolved? Was expecting it to be 'Any'
scala> val b : (Float, Float) = {val a : (Float, _) = (1f, 222); (a._1, a._2)}
-- [E007] Type Mismatch Error: <console>:5:67 ----------------------------------
5 |val b : (Float, Float) = {val a : (Float, _) = (1f, 222); (a._1, a._2)}
  |                                                                 ^^^^
  |                                                           found:    a.T2
  |                                                           required: Float
  |
scala> val b : (Float, Any) = {val a : (Float, _) = (1f, 222); (a._1, a._2)} // this WORKS !
val b: Float, Any = (1.0,222)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions