Function Literals with Receiver


#1

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


#2

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


#3

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


#4

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.


#5

thank