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

explicit "PolyFunction" type annotation cause ClassCastException #8299

Closed
xuwei-k opened this issue Feb 12, 2020 · 5 comments · Fixed by #18457
Closed

explicit "PolyFunction" type annotation cause ClassCastException #8299

xuwei-k opened this issue Feb 12, 2020 · 5 comments · Fixed by #18457

Comments

@xuwei-k
Copy link
Contributor

xuwei-k commented Feb 12, 2020

minimized code

dotty version: 0.23.0-bin-20200211-5b006fb-NIGHTLY

package example

object Main {
  def main(a: Array[String]): Unit = {
    val p: PolyFunction = [A] => (xs: List[A]) => xs.headOption
  }
}

sbt run log

[error] (run-main-5) java.lang.ClassCastException: example.Main$$anon$1 cannot be cast to scala.PolyFunction
[error] java.lang.ClassCastException: example.Main$$anon$1 cannot be cast to scala.PolyFunction
[error] 	at example.Main$.main(A.scala:5)
[error] 	at example.Main.main(A.scala)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] 	at java.lang.reflect.Method.invoke(Method.java:498)

expectation

compile error or no Exception.

@exoego
Copy link

exoego commented Jul 23, 2021

This seems resolved,
It compiles without error on 3.0.0 and 3.0.1.

@xuwei-k
Copy link
Contributor Author

xuwei-k commented Jul 23, 2021

$ scala --version
Scala code runner version 3.0.1 -- Copyright 2002-2021, LAMP/EPFL
$ scala
scala> val p: PolyFunction = [A] => (xs: List[A]) => xs.headOption
java.lang.ClassCastException: repl$.rs$line$1$$anon$1 cannot be cast to scala.PolyFunction
  at repl$.rs$line$1$.<clinit>(rs$line$1:1)
  at repl$.rs$line$1.p(rs$line$1)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at dotty.tools.repl.Rendering.$anonfun$3(Rendering.scala:115)
  at scala.Option.map(Option.scala:242)
  at dotty.tools.repl.Rendering.valueOf(Rendering.scala:115)
  at dotty.tools.repl.Rendering.renderVal(Rendering.scala:152)
  at dotty.tools.repl.ReplDriver.$anonfun$13(ReplDriver.scala:323)
  at scala.collection.immutable.List.flatMap(List.scala:293)
  at scala.collection.immutable.List.flatMap(List.scala:79)
  at dotty.tools.repl.ReplDriver.extractAndFormatMembers$1(ReplDriver.scala:323)
  at dotty.tools.repl.ReplDriver.renderDefinitions$$anonfun$2(ReplDriver.scala:346)
  at scala.Option.map(Option.scala:242)
  at dotty.tools.repl.ReplDriver.renderDefinitions(ReplDriver.scala:349)
  at dotty.tools.repl.ReplDriver.compile$$anonfun$2(ReplDriver.scala:268)
  at scala.util.Either.fold(Either.scala:189)
  at dotty.tools.repl.ReplDriver.compile(ReplDriver.scala:285)
  at dotty.tools.repl.ReplDriver.interpret(ReplDriver.scala:212)
  at dotty.tools.repl.ReplDriver.loop$1(ReplDriver.scala:146)
  at dotty.tools.repl.ReplDriver.runUntilQuit$$anonfun$1(ReplDriver.scala:149)
  at dotty.tools.repl.ReplDriver.withRedirectedOutput(ReplDriver.scala:168)
  at dotty.tools.repl.ReplDriver.runUntilQuit(ReplDriver.scala:149)
  at dotty.tools.repl.ReplDriver.tryRunning(ReplDriver.scala:115)
  at dotty.tools.repl.Main$.main(Main.scala:6)
  at dotty.tools.repl.Main.main(Main.scala)

@exoego
Copy link

exoego commented Jul 23, 2021

Ah, sorry.
It compiles, but throws on run.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Sep 7, 2023
`PolyFunction` must be refined with an `apply` method that has a single
parameter list with no by-name nor varargs parameters. It may optionally
have type parameters. Some of these restrictions could be lifted later,
but for now these features are not properly handled by the compiler.

Fixes scala#8299
Fixes scala#18302

fixup
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Sep 19, 2023
`PolyFunction` must be refined with an `apply` method that has a single
parameter list with no by-name nor varargs parameters. It may optionally
have type parameters. Some of these restrictions could be lifted later,
but for now these features are not properly handled by the compiler.

Fixes scala#8299
Fixes scala#18302

fixup
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 12, 2023
`PolyFunction` must be refined with an `apply` method that has a single
parameter list with no by-name nor varargs parameters. It may optionally
have type parameters. Some of these restrictions could be lifted later,
but for now these features are not properly handled by the compiler.

Fixes scala#8299
Fixes scala#18302
@xuwei-k
Copy link
Contributor Author

xuwei-k commented Dec 23, 2023

#18457 (comment)

please re-open this issue. or should I create new issue? 👀

@som-snytt
Copy link
Contributor

som-snytt commented Dec 23, 2023

@xuwei-k

Welcome to Scala 3.4.0-RC1-bin-SNAPSHOT-git-beaf7b4 (21, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> val p: PolyFunction = [A] => (xs: List[A]) => xs.headOption
-- Error: --------------------------------------------------------------------------------------------------------------
1 |val p: PolyFunction = [A] => (xs: List[A]) => xs.headOption
  |       ^^^^^^^^^^^^
  |       PolyFunction subtypes must refine the apply method
1 error found

Open a new ticket for the new issue you show. 😄

WojciechMazur added a commit that referenced this issue Jun 19, 2024
`PolyFunction` must be refined with an `apply` method that has a single
parameter list with no by-name nor varargs parameters. It may optionally
have type parameters. Some of these restrictions could be lifted later,
but for now these features are not properly handled by the compiler.

Fixes #8299
Fixes #18302

[Cherry-picked e5ca0c4][modified]
WojciechMazur added a commit that referenced this issue Jun 20, 2024
`PolyFunction` must be refined with an `apply` method that has a single
parameter list with no by-name nor varargs parameters. It may optionally
have type parameters. Some of these restrictions could be lifted later,
but for now these features are not properly handled by the compiler.

Fixes #8299
Fixes #18302

[Cherry-picked e5ca0c4][modified]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants