Christian Sousa
07/20/2020, 4:31 PMtype CustomReturnType = (a: String, b: Int) => String
export class Foo {
private aggregator: CustomReturnType[] = [
(a: String): String => a,
(a: String, b: Int) => {
...
return "something"
}
]
}
On Kotlin, I did something like this:
class Foo {
var aggregator: MutableList<(a: String, b: Int) -> String> = mutableListOf()
fun first(a: String): String {
return a
}
fun second(a: String, b: Int) {
...
return "something"
}
constructor() {
aggregator.add(first)
aggregator.add(second)
}
}
Problem is I’m getting a Type mismatch
Required: (String, Int) -> String
Found: String
Does anyone know why I can’t or is there something I’m doing wrong?Tobias Berger
07/20/2020, 4:44 PMundefined
(of course typescript could prevent this). If you put more parameters in the function call, they are just ignored. So in JS/TS, a function that only requires a single string parameter satisfies a declaration that provides an additional parameter (of any type).
In Kotlin - like in most languages - functions aren't that flexible and can't be called with more or fewer parameters. For you to put a function reference in that list, it has to exactly match the declared signature, which first
doesn't.{ a, _ -> first(a) }
Christian Sousa
07/21/2020, 9:58 AM