3

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?

Community
  • 1
  • 1
crockpotveggies
  • 12,682
  • 12
  • 70
  • 140
  • 1
    What happens when you replace `classTag[T].runtimeClass` with `classTag[T].runtimeClass.asInstanceOf[Class[T]]` in the call to `fetch`? – wingedsubmariner Jun 12 '14 at 21:55
  • @wingedsubmariner ah good call, looks like that did the trick. what a really weird issue though, that adds a lot of strange complexity – crockpotveggies Jun 12 '14 at 22:08

1 Answers1

7

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

new JSONConverter(classTag[T].runtimeClass.asInstanceOf[Class[T]], bucketName(clientId))
wingedsubmariner
  • 13,350
  • 1
  • 27
  • 52
  • ugh sorry unchecked, since the issue seems to cascade when I try to actually use `JSONConverter` objects, I'll update the question – crockpotveggies Jun 12 '14 at 21:43