guys if I have two final classes (generated from p...
# announcements
a
guys if I have two final classes (generated from package) of different types and they are following builder pattern and I have two other classes (generated from a package) that I want to them to be mapped to the builder classes I will map this manually, and they both have the same structure when mapping, can I have a one generic map method for both of them ?
Copy code
class Mapper
{
// can I have a one generic method, instead of two ??  
 like:   fun <T> MapKeyword (<U> keyword){ return T().SetId(keyword.Id).Build() }



  fun MapKeywordOne(KeywordOne  keywordOne) : keyWordOneBuilder  
    { return keyWordOneBuilder().SetId(keywordOne.Id).Build() }

   fun MapKeywordTwo(KeywordTwo  keywordTwo) : keyWordTwoBuilder  
    { return keyWordTwoBuilder().SetId(KeywordTwo.Id).Build() }
}



final class keyWordOneBuilder  // this one is from another package and I cannot modify
{   
setId(){ // implementation }
build(){ // implementation }
}

final class keyWordTwoBuilder  // this one is from another package and I cannot modify
{   
setId(){ // implementation }
build(){ // implementation }
}

final class KeywordOne  // this one is also sealed from a different package
{
public int Id;
}

final class KeywordTwo   // this one is also sealed from a different package
{
public int Id;
}
t
You could likely make this work with
reified
and a bit of reflection magic to create an instance, but that would rely on a no-arg constructor.
a
thanks a lot @Tyler Hodgkins yeah, I thought of reflection, but looks like I will add more complexities to this 😄
a
Perhaps making them both implement the same Interface and then "mapping" the Intrface would work?
t
AB mentioned that he doesn't own the classes
👍 1