twisterrob
08/11/2018, 9:35 AMfun main(vararg args: String) {
fun toCall(arg: String) = println("fun: $arg")
val toCall = fun (arg: String) = println("val: $arg")
toCall("hello")
}
What happens? (spoilers in thread)
1: outputs "fun: hello"
2: outputs "val: hello"
3. fails to compiletwisterrob
08/11/2018, 9:35 AMkarelpeeters
08/11/2018, 9:40 AMtoCall.invoke("hello")
it uses the val.karelpeeters
08/11/2018, 9:41 AMlouiscad
08/11/2018, 9:43 AMinvoke(…)
is the way to call the val
version. It reminds me how an extension can be shadowed by a member. This is a similar story here, where the hard defined function wins over the syntactic sugar for the invoke
operator on a functional type.twisterrob
08/11/2018, 9:45 AM(toCall)("hello")
also uses the val
, I guess that's the forced version of .invoke()
karelpeeters
08/11/2018, 9:46 AMtwisterrob
08/11/2018, 9:46 AMkarelpeeters
08/11/2018, 9:48 AMkarelpeeters
08/11/2018, 9:49 AMtwisterrob
08/11/2018, 11:53 AMkarelpeeters
08/11/2018, 11:55 AMval v = 5
val v = "hey"
println(v.subString(2))
to work either.twisterrob
08/11/2018, 11:59 AMval
is getToCall
while fun
is just toCall
there's no conflict in .class
twisterrob
08/11/2018, 12:00 PMkarelpeeters
08/11/2018, 12:00 PMtwisterrob
08/11/2018, 1:01 PMfun f(toCall: (String) -> String) {
fun toCall(arg: String): String =
"fun: $arg"
val toCall = fun(arg: String) =
"val: $arg"
println(toCall("called"))
}
karelpeeters
08/11/2018, 1:02 PMtwisterrob
08/11/2018, 1:18 PMtwisterrob
08/11/2018, 1:18 PM