Hello. I come from iOS development and am used to ...
# android
p
Hello. I come from iOS development and am used to calling kotlin lambdas (e.g.
val onAction: () -> Unit = { ... }
) as blocks. Is this naming clear to android developers? The word lambda in my head does not sound very nice in this context.
r
I don’t think it is going to be clear as block since there is initialisation block too. It would be better if you think it as someone calling closure’s in iOS as block and how clear that is going to sound.
e
there are some cases where it needs to be used but in general I would say that is not Kotlin style. lambda expressions are almost always passed directly to another higher-order function; if a local function is useful for commonizing code, then just define a local function using the usual
fun
syntax.
v
And actually only the
{ .... }
part is the lambda.
val onAction
is the declaration of a read-only variable, field or property.
() -> Unit
is a function type that is the type of the variable.
☝️ 1
a
@ephemient I think the question was "will Android devs understand what I mean when I refer to
val onAction: () -> Unit = { ... }
by the word _block_"
e
no
v
no
e
the
{ .. }
syntax forms a lambda expression, which may translate to an instance of a function or SAM type, or get inlined into no instance at all, depending on context. the word "block", if used, I'd expect to mean some scoped unit of code (as in Java)
☝️ 2
p
Thanks everyone!
a
Just to be contrary though, it's extremely common to see
block: () -> Unit
in kotlin stdlib functions as well as Android-centric libraries, especially in the case of inline functions where the parameter is called in place before the function returns
So while it's not necessarily precise, people will generally know what you mean unless they're specifically asking about language specifics as part of their question
v
But what is wrong with just using the correct terminology, especially if you know but just don't like it?
a
Not much, but most people play fast and loose with the term, "lambda" too. 🙂
{ doSomethingWith(someVal) }
is a lambda expression,
() -> Unit
is a function type. Some people use the word lambda when referring to function types even when that's not fully correct, etc.
You pointed that out above 🙂