https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

Sajal

01/16/2023, 2:29 PM
Hello everyone, I have just started using Kotlin at work. Can someone explain what this line of code mean (declaring a method argument): interface Contract fun someMethod(someLambda: Contract.() -> Unit) { } Here we are declaring a method that takes a lambda
someLambda
, where the lambda must take in a
Contract.()
and return nothing (
Unit
). But what does
Contract.()
mean? Are we dot-accessing something on an interface?
e

ephemient

01/16/2023, 2:32 PM
https://kotlinlang.org/docs/lambdas.html#function-types
Function types can optionally have an additional receiver type, which is specified before the dot in the notation
✔️ 4
s

Sajal

01/16/2023, 2:52 PM
I was looking for a name of this syntax to read up on it. Thanks.
t

Tung97 Hl

01/17/2023, 3:31 AM
someLambda is the parameter with the type of function: Contract.() mean the someLambda must be an extension function of type Contract, and someLambda is the function which returns Unit.
e

ephemient

01/17/2023, 3:39 AM
not quite. it doesn't need to be an extension function:
Copy code
val someLambda: (Contract) -> Unit = { ... }
someMethod(someLambda)
will compile and run just fine. what it does do, though, is make it so that when you write a lambda right there, it is inferred to have a receiver of type `Contract`:
Copy code
someMethod {
    this is Contract
}
t

Tung97 Hl

01/17/2023, 4:06 AM
@ephemient thanks for correct me
plus1 1
2 Views