softshipper
softshipper

Reputation: 34081

java.lang.Object cannot be converted to boolean

I call a function in the stream, that expression a predicate as return value:

KTable<String, String> store = source
.filter((key, value) -> Path.store().apply(value));

The Path.store is written in Scala and looks as following:

def store: String => Boolean =
  (v) =>
    encode(v).error.isEmpty

and the compiler complains:

[error]     java.lang.Object cannot be converted to boolean
[error]     .filter((key, value) -> Path.store().apply(value));

Do I have to convert to primitive?

Upvotes: 1

Views: 2031

Answers (1)

G&#225;bor Bakos
G&#225;bor Bakos

Reputation: 9100

The reason you see java.lang.Object as the result of apply in Java is originated from the richer type system of Scala. Function1[-T1, +R] is covariant in its return type, which cannot be expressed on the JVM in a typesafe way that Java can also interpret, so the return type of the apply is going to be Object.

You need to cast it back to Boolean (I hope I did not make a syntactic mistake):

source.filter((key, value) -> ((Boolean)Path.store().apply(value)).booleanValue());

Upvotes: 1

Related Questions