Is it possible for a class to extend 2 lambdas som...
# getting-started
u
Is it possible for a class to extend 2 lambdas somehow, given they are interfaces?
class ApiReactionMapper() : (ApiReaction?) -> Reaction?, _(Reaction?) -> ApiReaction?_ {
p
It seems that you cannot implement the same interface twice with kust different generic type parameter. So if two function types will have different number of parameters, then you can implement them both.
u
yea I figured that, but doesnt work if the latter one is (String, String) -> String either
t
It seems that you can't implement multiple lambda interfaces that have the same number of parameters and not the same return value. You may find the following code useful depending on what you want to do :
Copy code
interface Mapper<T, U> {
    fun transform(element: T): U
    fun reverse(element: U): T
}

@JvmName("transform")
operator fun <T, U> Mapper<T, U>.invoke(element: T): U = transform(element)
@JvmName("reverse")
operator fun <T, U> Mapper<T, U>.invoke(element: U): T = reverse(element)

class StringToIntMapper : Mapper<String, Int> {
    override fun transform(element: String): Int = element.toInt()
    override fun reverse(element: Int): String = element.toString()
}

fun main() {
    val mapper = StringToIntMapper()
    val asInt = mapper("42")
    val asString = mapper(asInt)
    println(asString)
}