hello, a question related to SAM functional interf...
# announcements
a
hello, a question related to SAM functional interfaces
Copy code
interface RequestAuth {
    fun header(): Pair<String, String>
}
val instance = object : RequestAuth {
                override fun header(): Pair<String, String> = "name" to "value"
            }
cant this be done more more concisely? sth like
Copy code
val instance = object : RequestAuth { "name" to "value" }
d
This will be supported in 1.4 with the new type inference. Until then you can do something like this:
Copy code
interface RequestAuth {
    fun header(): Pair<String, String>
    companion object {
        inline operator fun invoke(crossinline body: () -> Pair<String, String>) = object : RequestAuth {
            override fun header() = body()
        }
    }
}
👍 2
🤩 1
m
Not until v1.4. In 1.4 you can create a
fun interface
that will work as a SAM-interface. Then you can do this:
Copy code
fun interface RequestAuth {
    fun header(): Pair<String, String>
}

val instance: RequestAuth = { "name" to "value" }
👍 2
Both answers at the exact same time 😛
a
thx...waiting 1.4 then....