Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sugar for tuples does not preserve singleton types. #837

Closed
scabug opened this issue May 1, 2008 · 7 comments
Closed

Sugar for tuples does not preserve singleton types. #837

scabug opened this issue May 1, 2008 · 7 comments
Assignees

Comments

@scabug
Copy link

scabug commented May 1, 2008

The following works:

scala> val x = "foo"
x: java.lang.String = foo

scala> Tuple2[x.type, x.type](x,x)
res0: (x.type, x.type) = (foo,foo)

But all of the following do not:

scala> val y = (x : x.type, x : x.type)
y: (java.lang.String, java.lang.String) = (foo,foo)

scala> val y : (x.type, x.type) = (x, x)
<console>:5: error: type mismatch;
 found   : (java.lang.String, java.lang.String)
 required: (x.type, x.type)
       val y : (x.type, x.type) = (x, x)
                                  ^

scala> val y : (x.type, x.type) = (x : x.type, x : x.type)
<console>:5: error: type mismatch;
 found   : (java.lang.String, java.lang.String)
 required: (x.type, x.type)
       val y : (x.type, x.type) = (x : x.type, x : x.type)
                                  ^
scala> val y : (x.type, x.type) = (x, x) : (x.type, x.type)
<console>:5: error: type mismatch;
 found   : (java.lang.String, java.lang.String)
 required: (x.type, x.type)
       val y : (x.type, x.type) = (x, x) : (x.type, x.type)
                                  ^
@scabug
Copy link
Author

scabug commented May 1, 2008

Imported From: https://issues.scala-lang.org/browse/SI-837?orig=1
Reporter: Geoffrey Alan Washburn (washburn)

@scabug
Copy link
Author

scabug commented Jul 13, 2011

@paulp said:
Slightly surprisingly, this looks unrelated to tuple sugar per se, but a consequence of type inference in a pattern context.

@scabug
Copy link
Author

scabug commented May 6, 2012

@retronym said:
Versions 2, 3, and 4 work with master.

scala> (x: x.type, x: x.type)
res3: (String, String) = (foo,foo)

scala> val y : (x.type, x.type) = (x, x)
y: (x.type, x.type) = (foo,foo)

scala> val y = (x : x.type, x : x.type)
y: (String, String) = (foo,foo)

scala> val y : (x.type, x.type) = (x, x)
y: (x.type, x.type) = (foo,foo)

scala> val y : (x.type, x.type) = (x : x.type, x : x.type)
y: (x.type, x.type) = (foo,foo)

scala> val y : (x.type, x.type) = (x, x) : (x.type, x.type)
y: (x.type, x.type) = (foo,foo)

@scabug
Copy link
Author

scabug commented May 30, 2013

@retronym said (edited on May 30, 2013 8:15:50 PM UTC):
Not sure if this is exactly what Paul was referring to, but here's an example of unwanted widening in a pattern match:

scala> val x = ""
x: String = ""

scala> (x match { case y => y }): x.type
<console>:9: error: type mismatch;
 found   : y.type (with underlying type String)
 required: x.type
              (x match { case y => y }): x.type
                                   ^
// scala/tools/nsc/transform/patmat/MatchTranslation.scala:134
val selectorTp = repeatedToSeq(elimAnonymousClass(selector.tpe.widen.withoutAnnotations))

@SethTisue
Copy link
Member

@milessabin is this of interest?

@milessabin
Copy link

@SethTisue yes it is.

@milessabin
Copy link

As of scala/scala#5310 the situation is,

scala> val x = "foo"
x: String = foo

scala> Tuple2[x.type, x.type](x,x)
res0: (x.type, x.type) = (foo,foo)

scala> val y = (x : x.type, x : x.type)
y: (String, String) = (foo,foo)

scala> val y : (x.type, x.type) = (x, x)
y: (x.type, x.type) = (foo,foo)

scala> val y : (x.type, x.type) = (x : x.type, x : x.type)
y: (x.type, x.type) = (foo,foo)

scala> val y : (x.type, x.type) = (x, x) : (x.type, x.type)
y: (x.type, x.type) = (foo,foo)

The widening in the val y = (x : x.type, x : x.type) case matches this example using Tuple2,

scala> Tuple2(x: x.type, x: x.type)
res1: (String, String) = (foo,foo)

which is the correct behaviour given the absence of <: Singleton bounds on Tuple2's type parameters and the lack of a target type. So I think the sugar is doing the right thing here too.

So I'm declaring victory on this one.

@milessabin milessabin removed this from the 2.13.0-RC1 milestone Mar 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants