Hello everyone. I am learning about inline functio...
# getting-started
f
Hello everyone. I am learning about inline functions and I am getting error with cast. Please see 🧵
j
The
action
parameter is defined to return
Unit
, so it cannot return
T
You should change it to return
T
and you don't even need a cast, nor the
reified
actually
r
For example, here's your code updated to take what @Joffrey said into account: https://pl.kotl.in/e5x4j4siw
f
Yeahh I notice about that, but I thought the
crossinline
allows action parameter return Unit
r
crossinline
has nothing to do with what it's allowed to return, just when it's allowed to return.
j
crossinline
is just about where you can use the lambda and local returns, it doesn't change the constraints on its type
f
please see that library. action parameter return Unit, and the function return T like I do https://github.com/JuulLabs/kable/blob/64a5acf28af5bc03312a81f9231b9a12b82a0443/core/src/androidMain/kotlin/Connection.kt#L50
j
That function doesn't try to get a
T
by calling
action()
, though. It's not that you can't declare
Unit
as return type. It's just that if you want a
T
out of it, it must be declared as such. In your case,
mutex.withLock { action() }
returns whatever
action()
returns. So
ret
gets the result of
action()
. • If
action
is
() -> Unit
, then
ret
can only be
Unit
. • If
action
is
() -> T
, then
ret
will be of type
T
f
ahhh ok. I will keep
suspend inline fun <reified T> execute(crossinline action: () -> T) : T
j
There should be no need for
reified
anymore, though
f
ok, thank you very much
Do you know some material beside the oficial documentation to study that?
@Joffrey I understood about the returns. I was confusing the things. Now I need to understand better the reified and crossinline keywords
tks @Ruckus
r
@Dave Leeds recently did a video on the subject:

https://youtu.be/T9sAlxqYFYc?si=JuU1oDVtsftI-NM9â–¾

f
ok, I undertood crossinline and reified as well. But in the example above
suspend inline fun <reified T> execute(crossinline action: () -> T) : T
the reified is not necessary anymore. Why??
The reason is the return of action is the same of execute function and it is not necessary handle it??? So I do not know its type
j
Yes. Reified is only necessary if you need to know something about
T
. Here you don't. You're just getting a value of type
T
from the call to
action()
, and then returning it from your function (which has return type
T
). The implementation of your function doesn't need to know what
T
is at compile time, it just needs to know that the value you return matches the return type of the function, which is the case.
The main use case for
reified
is to get the class of
T
using
T::class
. Most of the time, this is used to wrap a function that needs a
Class<T>
into a function that gets this information from the reified
T
. This allows to call
myFun<Something>()
instead of
myFun(Something::class)
, which looks nicer and more idiomatic.
f
ok, all is clear for me now. Thank you so much
j
My pleasure!