Actually I need to detect 2 generic classes. `sour...
# codereview
m
Actually I need to detect 2 generic classes.
source
&
destination
. is it the right way to do this? Currently, I'm checking the list of contents is
DataA return ResultA
or
DataB return ResultB
. But here's my actual usecase:
Copy code
typealias ListMovieEntity = List<MovieEntity>
typealias ListMovieResponse = List<MovieResponse>

class MovieEntityMapper {

    companion object {
        fun <S : Any, D : Any> from(movies: ListMovie<S>): List<D> = movies.map {
            with(it) {
                when (this) {
                    is MovieEntity -> Movie(id)

                    is MovieResponse -> MovieEntity(id)

                    else -> TODO()
                } as D
            }
        }
    }
}
I'm doing this because, I can't do the overload on both kotlin / java due to type erasure in the runtime. e.g:
Copy code
fun fun1(list: List<A>)
fun fun2(list: List<B>)

// this two function will clashes each other.
s
You could also replace returning List<A> and List<B> with an object like
Copy code
data class AList(val data: List<A>)
data class BList(val data: List<B>)

fun fun1(list: AList);
fun fun1(list: BList);
@Timmy yea, there was a reply about this yesterday, but it looks like for some reason it was not a good fit.