Question

I use scala and have a case class defined as

case class caseClass(aString :String, bInt :Int, cClass : Class[_], dClass : Class[_], eClass : Class[_])

and then I have code to get a set of M from a list of classes

classes.filter(
  clazz => clazz.isInterface
).map(
    klazz =>  (
      klazz.getDeclaringClass,
      klazz,
      klazz.getDeclaringClass.getDeclaredClasses.filter(
        klass => klass.getSimpleName.equals("PythonJ")
      ).head
      )
  ).zipWithIndex.map { case ((service, iteratr, privte), port) => caseClass(
    s.getName, port, service, iteratr, privte
  )
}.toSet

But when I try to compile this code it gives an error saying

constructor of type (T1, T2, T3) cannot be uniquely instantiated to expected 
type Tuple3 [Class[?], Class[?], Class[?]]

 --- because ---

 undetermined type

 ).zipWithIndex.map { case ((service, iteratr, privte), port) => caseClass(
                             ^

Can someone tell me what I have to do to make this correct

Was it helpful?

Solution

Aurélien Thieriot is right, you need to assist the compiler - but in a different place:

classes.filter(
  clazz => clazz.isInterface
).map(
    klazz =>  (
      klazz.getDeclaringClass,
      klazz,
      klazz.getDeclaringClass.getDeclaredClasses.filter(
        klass => klass.getSimpleName.equals("PythonJ")
      ).head
      ): (Class[_],Class[_],Class[_]) // <--type spec
  ).zipWithIndex.map { case ((service, iteratr, privte), port) => caseClass(
    service.getName, port, service, iteratr, privte
  )
}.toSet

This will (in my understanding) help by fixing the type parameters of the result of map into existential types, which makes the subsequent type inference result unambiguous (i.e. the compiler will not be forced to infer "Class of what?").

OTHER TIPS

I would bet that the compiler is not able to infer the type.

Did you try by specifying the type you are expecting? Something like:

).zipWithIndex.map { case ((s: Class[_], i: Class[_], p: Class[_]), port) => Module(
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top