Replies: 3 comments 2 replies
-
Actually, we probably don't need to generate the synthetic class for the interface. As, they are already that type, duh 😬 |
Beta Was this translation helpful? Give feedback.
-
These are some examples of what we can generate: fun People.copyToLocalPerson(): LocalPerson =
LocalPerson(name = name, age = age)
fun People.copyToRemotePerson(): RemotePerson =
RemotePerson(name = name, age = age) Or, we could make them aliases of Use case: class PeopleRepository {
suspend fun itsbirthdayTime(person: Person) =
database.save(person.copyAsLocalPerson { age++ })
} Or, in some future: context(PeopleScope)
fun Person.itsbirthdayTime() = database.save(person.copyAsLocalPerson { age++ }) |
Beta Was this translation helpful? Give feedback.
-
Here's another idea: generate a fun LocalPerson.Companion.from(p: Person) = LocalPerson(...)
fun RemotePerson.Companion.from(p: Person) = RemotePerson(...) so you would be able to translate one to the other by doing What I like about this approach is that it makes clear that the copying "belongs" to |
Beta Was this translation helpful? Give feedback.
-
There is a use case that could help. Although I'm not sure if it falls outside this library's scope.
Let's say we have a type definition like this (potentially on different modules):
This is a good pattern to separate concerns without leaking external dependencies (in this case Room and kotlinx-serialization)
If we want to convert from and to another, we must create 'from' and 'to' functions. It's a lot of boilerplate.
So, generating these converter functions would be relatively painless.
We could pick up any data class that has an interface (or more) and generate the converter functions (
copyToLocalPerson()
?)The other thing for this is that we also need to create a plain type for the interface so we can convert to that type from the subtypes.
Beta Was this translation helpful? Give feedback.
All reactions