When declaring a higher order function (that accep...
# codingconventions
m
When declaring a higher order function (that accepts a lambda as argument) where you don’t use (and don’t care about) the return value of the lambda, do you put
Any?
or
Unit
for the return type? I guess
Any?
is the appropriate type, but I wonder if it adds confusion for example in private functions where you know the return type of the lambda is always
Unit
. 1.
Any?
always 2.
Unit
always 3.
Any?
when public,
Unit
when private and possible
2️⃣ 6
t
type expression based on lambda return type,
Copy code
fun <T> foo(..., f : (...)->SomeTypeExpression1<T>) : SomeTypeExpression2<T>
Unless you never use returned value, than
Unit
e
2. even if the caller has a lambda that returns non-Unit, they can always wrap it
{ foo() }
when passing it on
m
Ok, but why not just use
Any?
so they don’t have to?
e
IMO your types should tell you something about what the function does. if it's only executed for the side effect, it should be marked as returning Unit. Any? might imply that the value is still used somehow.
2