Hi, I am not sure where to ask that but I am exper...
# getting-started
p
Hi, I am not sure where to ask that but I am experiencing some difficulties to understand why the code below is not compiling. https://pl.kotl.in/G7jS_usIi I have the very same solution in our Android project from another developer and it’s working.
mapToDomain
is resolved and the project compiles. Could you please help with that?
We are using
@Parcelize
and
@Serializable
, but I don’t think it has influence on that
j
You need to have both receivers for this function to be resolved. At the moment you're providing
apiFoo
as the extension receiver, but you also need an instance of
DomainMapper<ApiFoo, *>
as a dispatch receiver. See: https://kotlinlang.org/docs/extensions.html#declaring-extensions-as-members
1
One way to do this is to bring the
ApiFooMapper
in the context by wrapping the call in `with(ApiFooMapper) { ... }`:
Copy code
val foo = with(ApiFooMapper) { apiFoo.mapToDomain() }
https://pl.kotl.in/-hYk5CWPc
I have the very same solution in our Android project from another developer and it’s working
Probably they call this function in a context where a
DomainMapper
is available as
this
(for instance within an extension function on
DomainMapper
or within an implementation of
DomainMapper
)
p
understood, in our project the
mapToDomain
is being imported directly as
import …mappers.ApiFooMapper.mapToDomain
, so is it applying the
with(ApiFooMapper)
automatically?
anyway, thank you 🙂
j
No, it shouldn't get any receiver automatically, even with such import. The receivers will be provided by the call site of the function. Take a look at where the function is called in your code. If it resolves properly, it means some instance of
DomainMapper
must be available as
this
in the place where it's called. If it's possible for you to post your code here, we can probably help and show you where it is.
EDIT: my bad, if
ApiFooMapper
is an
object
in your code, then the
import …mappers.ApiFooMapper.mapToDomain
might actually be the reason why it works. It imports the method and its containing object implicitly
🖖 1
💥 1