Hey there. I have following code ``` private su...
# announcements
d
Hey there. I have following code
Copy code
private suspend fun upsertAssets(code: String, assets: List<CreateAssetCommand>): List<UpsertAssetResponse> {
        return assets.chunked(100)
            .map { akeneoClient.upsertAssets(code, it) }
            .fold(mutableListOf()) { acc, next -> acc + next }
    }
that produces the compile time error
Type mismatch: inferred type is List<UpsertAssetResponse> but MutableList<UpsertAssetResponse> was expected
(the fold line). What am I missing?
d
The
+
doesn't do what you think it does.
t
I think you can replace your fold with
flatten()
.
d
flatten sounds good. Would still love to understand why it's not working.
And when checking the code .plus() does exactly what I expect and I assume + is overloaded correctly.
t
Because when you call
+
, you get back a
List<T>
, not a
MutableList<T>
. Since your fold would be recreating the lists anyway, if you seed it with
listOf()
instead of
mutableListOf()
, that would work.
(But again, this is what
flatten
does for you!) 🙂
d
already changed it to flatten.. still wanted to understand what's wrong 😉
thx
j
s/map/flatMap
-> profit
t
Oh man, I spaced out. Yeah, totally.
flatMap
over
map
+
flatten
. Must have not had enough coffee. 😄
K 2
☕ 1