Why is it possible to call a `@Composable` functio...
# compose
h
Why is it possible to call a
@Composable
function from a
.map{}
but not
.flatMap{}
?
s
Doesn’t sound right. Are you sure that is what’s happening, and not just that you are not fulfilling the needs of the flatmap lambda which is to return an Iterable? While map can just return Unit (the generic gets inferred as Unit) which is also what your composables return anyway
h
Our situation is little different from using the Compose UI. This is related to the Compose Runtime where we have a compose function returning
data class
. Oddly enough if we I just write this extension function and use this instead of
Map.flatMap
it works
Copy code
public inline fun <K, V, R> Map<out K, V>.composableFlatMap(transform: (Map.Entry<K, V>) -> Iterable<R>): List<R> {
    return flatMapTo(ArrayList<R>(), transform)
}
s
Aha, well I don’t know what you mean by “doesn’t work” then, is it a compile time error, is it red in the IDE? While this function doesn’t do that? Super odd if so.
h
Its it's a lint error, yes.
s
What does it say? 😅
h
image.png
And the function is flagged
@Composable
l
Could you show us more context of your code?
h
I'll try to write up an example that creates the same issue
a
The built-in
.flatMap
should work the same way as
.map
.
inline fun
are pretty neat in that they allow calling back and forth from colored functions like
suspend
and
@Composable
without needing to be either themselves
j
Ya, that's what I would expect too. I just wrote this code snippet to confirm that you cannot invoke a
@Composable
from
flatMap
but funny enough you can wrap
flatMap
and it works!
Copy code
@Composable
fun SimpleComposable() {}

inline fun <T, R> Iterable<T>.flatMapWrapper(transform: (T) -> Iterable<R>): List<R> = flatMap(transform)

@Composable
fun Run() {
  listOf(1, 2, 3)
    .map { SimpleComposable() } // works
    .flatMap { listOf(SimpleComposable()) } // compilation error
    .flatMapWrapper { listOf(SimpleComposable()) } // works
}
h
@Alex Vanyo Just wanted to see if you saw the same error in the code snippet posted by John? It is doing the same error I mentioned.
a
Yep I can repro with that, it definitely looks like there is a bug somewhere there. Can you report one with that repro?
j
l
That's weird.
flatMap
is definitely
inline
.
h
@Alex Vanyo Sorry to ping you, but do you have a link to the official repo to report that to?
a
I believe it was already reported in the message above, thanks for following up!