Skip to content

Weird case of double-parentheses impacting type inference in List.apply #10062

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

Closed
neko-kai opened this issue Oct 21, 2020 · 3 comments
Closed

Comments

@neko-kai
Copy link
Contributor

Minimized code

class X

object X {
  extension (x: List[X]) { def isNull = x.head == null }
}

@main def test = List(null: X).isNull

https://scastie.scala-lang.org/He9Pm96ySZCBCziHLktlXg

Output

Error:

Found:    X
Required: Any*

Expectation

Should compile. If a redundant second set of parentheses is added in List((null: X)), then the example compiles:

class X

object X {
  extension (x: List[X]) { def isNull = x.head == null }
}

@main def test = List((null: X)).isNull

https://scastie.scala-lang.org/dwFH7NAsQyqKpoXHuzAWiA

@som-snytt
Copy link
Contributor

som-snytt commented Oct 21, 2020

scala> List(null: String)
1 |List(null: String)
  |     ^^^^^^^^^^^^
  |     Found:    String
  |     Required: Any*

scala> List("": String)
val res4: List[String] = List("")

scala> List((null: Seq[String]): _*)
java.lang.NullPointerException: Cannot invoke "scala.collection.IterableOnce.knownSize()" because "prefix" is null

Scala 2 doesn't fare as well with List(null: _*).

@neko-kai
Copy link
Contributor Author

@som-snytt
Hmm, works for me on Scala 2.13.3, your first repl command & scastie (https://scastie.scala-lang.org/yYKd7yxWTdSeKF1wezvFYQ).

class X

object App extends App {
  val test = List(null: X)
  println(test)
}

Is your repl printout from 2.12?

@som-snytt
Copy link
Contributor

Sorry I didn't add commentary. It's null sequence arg that is the problem for scala 2, which scala 3 handles the same as if it were null: Seq[_].

I was curious why it says Any* is required. The sequence arg annotation is just an ascription that can only be applied to the last argument in an application. I was curious about other positional problems and opened the linked ticket.

The other point was that the error here happens on List(null: String), which scala 2 is fine with. Maybe scala 3 already checked null <:< Seq[_]? I would think you have to look at _* even to understand what the application is, see the linked ticket.

@odersky odersky self-assigned this Nov 8, 2020
nicolasstucki added a commit that referenced this issue Nov 9, 2020
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

4 participants