1

I am having this function defined in a kotlin file

fun loadSubmissions(projectId: Long?, completion: (List<Submission>, Exception) -> Unit) { ... }

And want to call it from Java like this

loadSubmissions(project.getProjectId(), (submissions, e) ->
    {
        updateSubmissions(submissions);
        return null;
    });

with

void updateSubmissions(List<Submission> submissionList)
{ .. }

But it gives me

Error:(226, 35) error: incompatible types: List<CAP#1> cannot be 
converted to List<Submission>
where CAP#1 is a fresh type-variable:
CAP#1 extends Submission from capture of ? extends Submission

As I realize the parameter of the lambda function seems to be List<CAP#> instead of the expected and defined List<Submission>

If i convert the class to java, i can easily call that function with the lambda callback.

What am I doing wrong

2

1 Answer 1

2

You have not disclosed the type of variable submissions, but the error message appears to indicate that it is List<? extends Submission>. This is a different type from List<Submission>, and references of the former type are not assignable to variables of the latter type (though the other way around is ok).

That presents at least two problems for you:

  • the lambda itself is not type-correct where it invokes updateSubmissions(), and
  • the lambda's type, which comprises its parameter types, is incompatible with the type of the second parameter of loadSubmissions().

To fix this, you need either to broaden the parameter types of those two methods or to narrow the type of variable submissions.

Sign up to request clarification or add additional context in comments.

4 Comments

how to narrow down the type of submissions in the lambda block. If i specify it to List<Submission> like that new Function2<List<Submission>, Exception, Unit>() it says wrong 2nd argument type
There is no typesafe way to narrow the type of submissions in the lambda block. It's type is determined by its declaration, and converting it to a narrower type requires a cast. You could create a different object of suitable type, say by new ArrayList<Submission>(submissions), but what I was suggesting was that you change the type of submissions in its declaration (if that's the alternative you select).
Mh okay, and why is it working like this with pure kotlin?
@martynmlostekk, that would depend on the kotlin use that works for you, and possibly on some kotlin-specific semantics. I cannot speak to the details as you have not presented the relevant working source. That anyway would be a separate question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.