This page is no longer maintained — Please continue to the home page at

How to investigate a wildcard type as per error instructions?

No replies
John Ky
Joined: 2009-10-06,
User offline. Last seen 42 years 45 weeks ago.
Hi Scala Users,
I am getting an error I do not understand:
type mismatch; found : java.lang.reflect.TypeVariable[java.lang.reflect.GenericDeclaration] required: java.lang.reflect.TypeVariable[org.sfx.reflect.GenericDeclaration#Underlying]   Note: java.lang.reflect.GenericDeclaration >: org.sfx.reflect.GenericDeclaration#Underlying, but Java-defined trait TypeVariable is invariant in type D. You may wish to investigate   a wildcard type such as `_ >: org.sfx.reflect.GenericDeclaration#Underlying`. (SLS 3.2.10)

The relevant code is:
      key match {        // ...         case underlying: JTypeVariable[JGenericDeclaration] => TypeVariable[GenericDeclaration](underlying)         // ...      }
The error is suggesting I investigate a wildcard type, but what does it mean, and how do I apply it to my code?
Any help appreciated.
For reference, the types I use are as follows:
// ------------ package org.sfx.reflect
import java.lang.{Class => JClass}import java.lang.reflect.{GenericDeclaration => JGenericDeclaration} import java.lang.reflect.{TypeVariable => JTypeVariable}import java.lang.reflect.{Type => JType}
trait TypeVariable[D <: GenericDeclaration] extends Type {   type Underlying <: JTypeVariable[_]  def underlying: JTypeVariable[_]     def bounds: Array[JType]   def genericDeclaration: D  def name: String }
object TypeVariable {  def apply[S <: GenericDeclaration](underlying: JTypeVariable[S#Underlying]): TypeVariable[S] = {     val u = underlying    return new TypeVariable[S] {       def underlying: JTypeVariable[_] = u      def bounds: Array[JType] = underlying.getBounds()       def genericDeclaration: S = underlying.getGenericDeclaration().asInstanceOf[S]      def name: String = underlying.getName()     }  } }

// ------------package org.sfx.reflect
import java.lang.reflect.{GenericDeclaration => JGenericDeclaration} import java.lang.reflect.{TypeVariable => JTypeVariable}
trait GenericDeclaration {  type Underlying <: JGenericDeclaration   def underlying: JGenericDeclaration     def typeParameters[S <: GenericDeclaration]: Array[TypeVariable[S]] = underlying.getTypeParameters().map { jTypeVariable =>    throw new RuntimeException("not implemented")   }}
object GenericDeclaration {}


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland