Search code examples
scalareflectiontypesscala-2.10jackson-modules

Scala Class[_$1] where type _$1


Right now trying to instantiate a new JSONConverter to register Jackson's Scala module.

  private def getConverter(implicit m: ClassTag[T]) = {
    new JSONConverter[T](classTag[T].runtimeClass, bucketName)
    JSONConverter.registerJacksonModule(DefaultScalaModule)
    converter
  }

The above code sits in a standard Scala trait that looks like trait Writeable[T] { }.

The problem with the above code is that Scala seems to be having a difficult time with Types. Compiler error is:

[error]  found   : Class[_$1] where type _$1
[error]  required: Class[T]
[error]         val converter = new JSONConverter[T](classTag[T].runtimeClass, bucketName(clientId))
[error]                                                          ^
[error] one error found

Anyone know the source or easy fix of this issue? Thanks!

Update

Although @wingedsubmariner had an answer that allowed this to originally compile, as soon as I went to write more code the issue cascaded further. I'll show an example:

val o = bucketLookup(clientId).fetch(id, classTag[T].runtimeClass).withConverter(converter).withRetrier(DB.retrier).r(DB.N_READ).execute()

At withConverter the compiler throws the same error:

[error]  found   : com.basho.riak.client.convert.JSONConverter[T]
[error]  required: com.basho.riak.client.convert.Converter[_$1] where type _$1
[error]     val o = bucketLookup(clientId).fetch(id, classTag[T].runtimeClass).withConverter(converter).withRetrier(DB.retrier).r(DB.N_READ).execute()

I even tried doing the same type casting using converter.asInstanceOf[JSONConverter[T]] but inheritance (JSONConverter<T> extends Converter<T>) seems to cascade the issue. Any ideas here?


Solution

  • runtimeClass is retuning a Class with the wrong type parameter. Try:

    new JSONConverter(classTag[T].runtimeClass.asInstanceOf[Class[T]], bucketName(clientId))