Can't create lambda function

Hi! I have a very easy question, but I really don’t understand how it works.

I have two interfaces:

interface Recipient

interface EmailRecipient : Recipient {
    fun getEmailAddress(): String
}

I want to create a builder function, that get a String and return an instance of EmailRecipient:

fun buildEmailRecipient(email: String): EmailRecipient {
    return object : EmailRecipient {
        override fun getEmailAddress(): String {
            return email
        }
    }
}

I would like to write it as lambda. But my code like

fun buildEmailRecipient(email: String): EmailRecipient 
                    = EmailRecipient {email}

doesn’t work.

EmailRecipient is a Supplier, isn’t it? And code like

fun buildSomeSupplier(s: String): Supplier<String> = Supplier { s }

works.

Can you help me please? Why my EmailRecipient interface can’t be present as lambda?

Take a look at function interfaces (also somtimes called SAM interfaces). That should answer your question :upside_down_face:
https://kotlinlang.org/docs/reference/fun-interfaces.html

The docs will explain how to use SAM conversions.
I do feel that doc page fails to explain how they differ from a real lambda literal.

In Java, lambdas aren’t first class citizens like they are in Kotlin. Java ends up having classes for lambda kinds like predicate and supplier.

On the other hand Kotlin has types for lambdas such as (T) -> R. And you don’t have to create a new named SAM type every time you a different number of arguments or return types. Classes can implement these types directly as well.

Thanks! I updated Kotlin version to 1.4 and now I can use fun prefix to interfaces.