Как я могу получить TypeInformation из универсального типа в Scala - PullRequest
0 голосов
/ 13 мая 2019

Я пытаюсь расширить DeserializationSchema для класса с универсальным типом

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

но я получаю

требуется тип класса, но T обнаружил переопределение def getProducedType: TypeInformation [T] = TypeInformation.of (classOf [T])

любая идея

Ответы [ 2 ]

0 голосов
/ 13 мая 2019

Вместо получения classOf из T вы можете потребовать неявное TypeInformation[T] как часть объявления Foo:

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 = ???
}
0 голосов
/ 13 мая 2019

Согласно документации

Для универсальных типов вам необходимо «захватить» информацию об универсальных типах через TypeHint:

Так что вы можете заставить его скомпилироваться следующим образом

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