2

I am injecting a kotlin class into my java code. The kotlin class has two methods with nearly identical signatures:

fun <R> isFluxAuthorizedFor(
                strings: List<StringRequest>,
                then: (responses: List<StringResult>) -> Flux<R>
        ): Flux<R> 

and

fun <R> isFluxAuthorizedFor(
                string: StringRequest,
                then: (per: StringResult) -> Flux<R>
        ): Flux<R> {

The kotlin class supports this overloading just fine.

However, I'm having a devil of a time getting my IDE to use the correct method. I have a method which matches the signature of the former:

private Flux<AuthorizedStrings> collectResults(List<StringResult> responses)
    {
        //not yet implemented
        return null;            
    }

And yet, when I try and call the injected class' method, I get compilation errors:

List<StringRequest> allStrings = new ArrayList<StringRequest>();
Flux<UserReadAuthorizations> test =  authCheck.isFluxAuthorizedFor(allStrings, (it) -> this.collectResults(it) ); 

The IDE makes two suggestions:

"change type of 'it' to 'List<StringResult>'"

"change method 'collectResults(List<StringResult>)' to 'collectResults(StringResult)'"

both of which imply that Java (or at least the compiler) can't figure out that I'm trying to call the other method. Is this a problem trying to integrate Java 8 and Kotlin? A quirk of the IDE? (I'm using Spring Tool Suite) Some silly user error that I've not yet been able to rubber-duck through?

Raven Dreamer
  • 6,940
  • 13
  • 64
  • 101

1 Answers1

1

I played with your code and found that IntelliJ tripped over the type of the lambda as a whole. I had to cast it -> this.collectResults(it) to the type Kotlin was expecting:

List<StringRequest> allStrings = new ArrayList<>();
Flux<UserReadAuthorizations> test = authCheck.isFluxAuthorizedFor(
    allStrings,
    (Function1<List<StringResult>, Flux<AuthorizedStrings>>) (it -> this.collectResults(it))
);

The signature of your methods themselves was not an issue. Hope this helps you in STS as well.

Malte Hartwig
  • 4,477
  • 2
  • 14
  • 30