Function Literals with Receiver

What different between Extension Functions and Function Literals with Receiver ?
Function Literals with Receiver - example from book sum : Int.(other: Int) → Int
use 1.sum(2)
Why I can’t write like this:
fun Int.sum(i:Int):Int=this+i

I’d say it’s the same difference as the difference between a regular (non-extension) function and a regular (no receiver) function literal.

ok, can I say that both of this functions extend and literal are resolve the same problem ?

Does a regular function solve the same problem as a lambda? I don’t think so.

In the same way, an extension function does not solve the same problem as a lambda with receiver.

A lambda with receiver is something that you can pass to another function, so that the other function would call it, and pass the receiver object that you would be able to access in the body of the lambda. An extension function is something that you can only call directly.

1 Like