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

Compiler error when mocking classes with generics #170

Open
barkhorn opened this issue Jan 23, 2017 · 6 comments
Open

Compiler error when mocking classes with generics #170

barkhorn opened this issue Jan 23, 2017 · 6 comments

Comments

@barkhorn
Copy link
Collaborator

worked in 3.4.2, broken in 3.5.0

real example:

      class MockableBroadcast[T: ClassTag] extends org.apache.spark.broadcast.Broadcast[T](42L) {
        override protected def getValue(): Nothing = ???
        override protected def doUnpersist(blocking: Boolean): Unit = ???
        override protected def doDestroy(blocking: Boolean): Unit = ???
      }
      val lookup= mock[MockableBroadcast[Map[String, Long]]]

minified example:

      class Foo[T: ClassTag]
      val mf = mock[Foo[Nothing]]

compiler error:

Error:(55, 20) type mismatch;
 found   : scala.reflect.ClassTag[T]
 required: scala.reflect.ClassTag[Nothing]
Note: T >: Nothing, but trait ClassTag is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: Nothing`. (SLS 3.2.10)
      val mf = mock[Foo[Nothing]]
@barkhorn barkhorn added this to the v3.5.1 milestone Jan 23, 2017
@barkhorn barkhorn changed the title compiler error when mocking classes with generics regression: compiler error when mocking classes with generics Jan 23, 2017
@barkhorn
Copy link
Collaborator Author

barkhorn commented Jan 23, 2017

cause: changes to MockMaker.initDef in e07cca3

@barkhorn
Copy link
Collaborator Author

symbols => symbols.map(_.typeSignatureIn(constructorTypeContext))

typeSignatureIn returns the parameter T, not the materialised type (e.g. Int)

@barkhorn barkhorn mentioned this issue Feb 4, 2017
@barkhorn
Copy link
Collaborator Author

barkhorn commented Feb 4, 2017

@barkhorn
Copy link
Collaborator Author

very stuck, the solution with .dealias is more difficult to implement than I thought. Also MockAdapter for 2.10 redirects this to .normalize

@barkhorn barkhorn removed this from the v4.3.0 milestone Jun 23, 2019
@barkhorn barkhorn removed the bug label Jan 2, 2021
@barkhorn barkhorn changed the title regression: compiler error when mocking classes with generics Compiler error when mocking classes with generics Jun 18, 2022
@kell18
Copy link

kell18 commented Jul 19, 2023

Hello, is there maybe any workaround for this one? It's quite painful... Thanks!

@kell18
Copy link

kell18 commented Jul 19, 2023

Okay, I found workaround, you need to specify all the types (including _ in F[_]) explicitly by creating another class which extends from the generic one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants