Q1. Slight confusion with the terms:
In the documentation https://kotlinlang.org/docs/reference/lambdas.html#instantiating-a-function-type
Non-literal values of function types with and without receiver are interchangeable, so that the receiver can stand in for the first parameter, and vice versa. For instance, a value of type
(A, B) -> C
can be passed or assigned where aA.(B) -> C
is expected and the other way around:val repeatFun: String.(Int) -> String = { times -> this.repeat(times) } val twoParameters: (String, Int) -> String = repeatFun // OK fun runTransformation(f: (String, Int) -> String): String { return f("hello", 3) } val result = runTransformation(repeatFun) // OK
In the code, are repeatFun
and twoParameters
“non-literal” function types?
What is the definition of “Non-literal” function types?
​
From my understanding,
function literals are either lambda expressions or anonymous functions.
Therefore I thought “non-literal” function types would be function types which are neither lambda expression nor anonymous function.
In the code
val repeatFun: String.(Int) -> String = { times -> this.repeat(times) }
{ times -> this.repeat(times) }
this is a lambda expression, which is a function literal.
What am I missing here?
Should I understand this differently, so that
{ times -> this.repeat(times) }
is a function literal,
but repeatFun
or twoParameters
are “non-literal”?
Q2.
More below in the documentation it’s stated
Note that a function type with no receiver is inferred by default, even if a variable is initialized with a reference to an extension function. To alter that, specify the variable type explicitly.
I couldn’t understand this statement. Could someone please give an example or elaborate it?