Hello everyone, I was playing around with val func...
# getting-started
m
Hello everyone, I was playing around with val functions and I ran into this problem ... could someone help me understand why the e option doesn’t work (which would seem the most reasonable) and why all the above works? https://pl.kotl.in/YdmU2qEUy
d
When you use trailing lambda syntax it is just syntax sugar for such call:
Copy code
foo(a, b, c) { bar() } 
foo(a, b, c, { bar() })
So when you call
f("hello, world") { it.toUpperCase() }
in your example is is desugared to
f.invoke("hello, world", { it.toUpperCase() })
, which is incorrect, because it should be
f.invoke("").invoke({ it.toUpperCase() })
☝️ 1
m
oka! perfect! that makes sense! Thank you!
with that in mind I understand all the examples except the A.. how does A works?
and I can add another option to the list
f("hello, world").invoke {it.uppercase()}.also { println("f: $it") }
d
Copy code
f("hello, world")() { it.uppercase() }
// same as
val x = f("hello, world")
x() { it.uppercase() }
m
that is example D, and there is no need to use the parentheses... even more, why I can add the parentheses if the
val a
has type
((String) -> String) -> String
e
f() == f.invoke()
(if
f
is a val, var, object)
f() {...} == f({...})  == f {...}
empty parens before lambda can be present or omitted
in that case,
Copy code
f1 { it.uppercase() }
f1() { it.uppercase() }
f1({ it.uppercase() })
f1.invoke { it.uppercase() }
f1.invoke() { it.uppercase() }
f1.invoke({ it.uppercase() })
are all equivalent
👏 1
m
perfect, crystal clear! thanks!