is there a way to say "this function executes a la...
# announcements
l
is there a way to say "this function executes a lambda. If the lambda is
suspend
, make the function
suspend
, otherwise don't". I know the same thing can be achieved using
inline fun
, but I have to access private fields in this function so that's not an option
🤔 1
d
You'll have to have two functions then, as a function signature changes if its
suspend
(it has to take an additional
Continuation
parameter)
What you can maybe do is have the public-facing function
inline
and encapsulate the private access in a
@PublishedApi internal fun
which does not suspend and doesn't use the lambda.
l
I'll try that, thanks! could this technically be something that we'll be able to do in the future? like, have an additional modifier or some other way to "inline" suspendability
d
It would have to compile down to two functions. Suspending and non-suspending functions result it quite different bytecode.
l
that would be an option. another option would be to still inline them but to somehow allow access of private properties in inline functions. Another question: Why is it that this works if the class containing the inline function is private and is declared within the same file it's used? In that case i can refer to the private fields from my inline fun
d
You have to keep in mind that inlining means just that: The function code is inlined into the code where it is called. That means a public inline function only has access to public things, because private things could not be accessed by whomever is calling that inline function.
l
this does make sense, altough I'd think it's pretty likely that JVM-stuff would allow this using some strange hacks (reflection would most likely work but would be a bad idea). But why does it work with private classes?
d
Because if the inline function is
private
(or in a
private
class) then it can only ever be inline in the same class / file, so wherever it is inlined to also has access to the same private fields.
Also yes, you could work around it using reflection, but keep in mind that usually
private
fields are not part of your public API. If you could access them inside an
inline
function they suddenly would be, because the code of your inline function gets copied into other people's code.
l
okay, thanks!