0

I have the following Scala trait that I would like to inherit:

trait StreamTableSink[T] extends TableSink[T] {

  /** Emits the DataStream. */
  def emitDataStream(dataStream: DataStream[T]): Unit
}

that extends:

trait TableSink[T] {

  private var fieldNames: Option[Array[String]] = None
  private var fieldTypes: Option[Array[TypeInformation[_]]] = None
  ...
}

But when I inherit it in Java like this:

public abstract class KafkaTableSink implements StreamTableSink<Row> {
  ...
}

I receive the following error:

Error:(29, 8) java: kafka.KafkaAvroTableSink09 is not abstract and does not override abstract method TableSink$$fieldTypes_$eq(scala.Option<typeinfo.TypeInformation<?>[]>) in sinks.TableSink

where KafkaAvroTableSink09 inherits KafkaTableSink

Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67
  • You could just make `fieldNames` and `fieldTypes` vals' instead of vars'. – marstran Jul 06 '16 at 07:06
  • That question is talking about `def`s, not `var`s, but the problem and the solution are the same. – Alexey Romanov Jul 06 '16 at 17:13
  • @AlexeyRomanov I don't think that is the duplicate of the question that you mentioned. I don't have any issues with the "def" and I successfully implemented the abstract method. I have an issue with the fieldTypes_$eq method which seems related to "fieldTypes" var to me. – Ivan Mushketyk Jul 06 '16 at 21:45
  • `var`s create two non-abstract methods, so you have a trait with non-abstract methods, exactly as in the linked question. Same will happen with non-abstract `val`s. – Alexey Romanov Jul 07 '16 at 06:30
  • @AlexeyRomanov Does it mean that it's impossible to inherit traits with "var"s? – Ivan Mushketyk Jul 07 '16 at 08:09
  • Again, the answer is the same: they can't be inherited directly, you need to write an abstract class extending the trait in Scala and inherit that class in Java. – Alexey Romanov Jul 07 '16 at 10:22

1 Answers1

1

Two problems might be arising.

1) java does not know what type to give fieldTypes due to the use of a placeholder type (underscore). This might lead to the generated fieldTypes_$eq method being made abstract (see here to see what java code is generated for a var)

2) scala traits cannot be extended in Java if they have any implementation details.

1 should be fixed by giving it an explicit type and 2 can be fixed by wrapping the trait in a class to extend in Java. (although 2 doesn't seem to be your problem, just something to be aware of)

  • No, the problem is 2. `_` in Scala is correctly translated into Java's `?` wildcard, which you can see in the error message. – Alexey Romanov Jul 06 '16 at 17:11