In a similar vein of "why this not that," I'm not ...
# getting-started
u
In a similar vein of "why this not that," I'm not finding a compelling explanation for the differentiated uses of = and ->. Specifically in the contexts of single expression functions vs. lambdas. They're both predicates to their respective return statements, and as far as I can see, serve the same purpose. So why the distinction in form? Why not just use arrow syntax in both cases?
y
I guess the distinction between
Copy code
var bar: Int
let {
  bar = 5
}
// vs
let {
  bar -> 5
}
matters. Also,
=
for single expression functions has no
{}
, but lambdas always have
{}
u
That's a fair point! My perspective is definitely limited. I'm thinking mostly of all the property extensions I'm writing in exercises right now. It feels arbitrary to right get() = instead of get() -> since the latter seems like a more idiomatic representation of "return".
j
But
->
is not "return", it just separates the parameters from a lambda's body (which could contain many statements before the returned expression). In function declarations (both
fun
and
get
), there are parentheses for parameters, so you don't need an extra separator. The
=
sign, on the other hand, always expects a single expression on the right-hand side - you can't have statements there. So it's about whether the body has `return`s or a single expression. Lambdas are neither of those (not necessarily a single expression, and no returns), so it makes sense to me that the syntax is different