0

I am trying to extend DeserializationSchema for class with generic type

class Foo[T] extends DeserializationSchema[T] {
...
  override def getProducedType: TypeInformation[T] = TypeInformation.of(classOf[T])
}

but I'm getting

class type required but T found override def getProducedType: TypeInformation[T] = TypeInformation.of(classOf[T])

any idea

igx
  • 4,101
  • 11
  • 43
  • 88
  • Possible duplicate of [How to get rid of : class type required but T found](https://stackoverflow.com/questions/26567370/how-to-get-rid-of-class-type-required-but-t-found) – Dmytro Mitin May 13 '19 at 10:15
  • [Class type required but T found](https://stackoverflow.com/questions/20591957/class-type-required-but-t-found/20604596) – Dmytro Mitin May 13 '19 at 10:17
  • [Scala: class type required but T found](https://stackoverflow.com/questions/24172958/scala-class-type-required-but-t-found) – Dmytro Mitin May 13 '19 at 10:19

2 Answers2

0

According to the documentation

For generic types, you need to “capture” the generic type information via the TypeHint:

So you can make it compile like this

class Foo[T] extends DeserializationSchema[T] {
...
  override def getProducedType: TypeInformation[T] = TypeInformation.of(new TypeHint[T]{})
}
Dionysis Nt.
  • 955
  • 1
  • 6
  • 16
0

Instead of deriving classOf from T, you can require an implicit TypeInformation[T] as part of Foo's declaration:

class Foo[T](implicit typeInformation: TypeInformation[T]) extends DeserializationSchema[T] {
  override def getProducedType: TypeInformation[T] = typeInformation

  override def deserialize(message: Array[Byte]): T = ???

  override def isEndOfStream(nextElement: T): Boolean = ???
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321