y
04/02/2025, 8:15 AMval myFun = { foo: Foo -> foo }
), as opposed to local functions (fun myFun(foo: Foo): Foo = foo
)?
unlike some other languages (e.g. Rust), local functions can capture local variables, so the local lambda syntax seems entirely redundant. local functions can also be recursiveKlitos Kyriacou
04/02/2025, 9:11 AMfun
style may look nicer to you, but if you want to use it as a method reference you have to prefix it with ::
whereas with a lambda you don't.
val myFun: (Foo) -> Foo = { foo: Foo -> foo }
fun myFun(foo: Foo): Foo = foo // Note there's no name clash, you can have both named the same!
listOf(Foo()).map(myFun) // Refers to the lambda
listOf(Foo()).map(::myFun) // Refers to the function
y
04/02/2025, 9:20 AMval
-style is that in practice it ends up being more bulky instead of less bulky, because too often input and output types are unable to be inferred
and explicit lambda syntax is (imho) "heavier" than function styley
04/02/2025, 9:22 AMephemient
04/02/2025, 10:25 AMval myFun = fun(foo: Foo): Foo = foo
ephemient
04/02/2025, 10:27 AM{ }
lambdas and not any fun
, although that's only usable when inlined (and thus not when captured in a local val
)ephemient
04/02/2025, 10:28 AM