Hello everyone, I have just started using Kotlin a...
# getting-started
s
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
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
I was looking for a name of this syntax to read up on it. Thanks.
t
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
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
@ephemient thanks for correct me
plus1 1