1

I have this function that (theoretically) accept an array of functions as argument.

fun execute(afterDay: Long, listeners: Array<(List<String>) -> Unit>)

In the client class I trying to call this execute method and pass functions as parameter:

fun updateMovies(ids: Array<String>){

}

fun getNewIds() {
    GetImdbIds(kodein).execute(daysBack.toEpochDay(), [::updateMovies])
}

But it doesn't compiles.

What I'm doing wrong?

the error:

Error:(29, 59) Kotlin: Type inference failed. Expected type mismatch: inferred type is Array<KFunction1<@ParameterName Array<String>, Unit>> but Array<(List<String>) -> Unit> was expected
Error:(29, 59) Kotlin: Unsupported [Collection literals outside of annotations]
1
  • he said 'unresolved reference' but I realized the function should be declared before. Now the error is: Error:(29, 59) Kotlin: Type inference failed. Expected type mismatch: inferred type is Array<KFunction1<@ParameterName Array<String>, Unit>> but Array<(List<String>) -> Unit> was expected Error:(29, 59) Kotlin: Unsupported [Collection literals outside of annotations] Commented Jan 25, 2019 at 0:39

1 Answer 1

5

I got this to work by making two changes.

First, your updateMovies function as written takes an Array<String>, when your listeners wants functions that take List<String>. So, we can make this change:

fun updateMovies(ids: List<String>) {
    TODO()
}

Next, if you create your array of function references using arrayOf() instead of attempting an illegal array literal, this should compile:

GetImdbIds(kodein).execute(1L, arrayOf(::updateMovies))
Sign up to request clarification or add additional context in comments.

Comments

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.