I am writing some classes that need to be instantiated from more than one object type on different occasions. These classes will all end up with the same fields no matter the object used to create it. For example I want to create a class instance from a Map that contains the values I need to set. I also want to create a class instance from another arbitrary object that also contains the same fields but naturally they must be acquired in a different way to the Map and the class is not the same type so cannot be passed into the same constructor.
I have a solution to this using a companion object and writing static methods to create instances of the class from each object. But I don’t like it very much.
What I would like to be able to do is have multiple constructors that take different objects as arguments and do not have to call the primary constructor, as these secondary constructors take totally different arguments and they cannot be shared with the primary constructor. I also do not want to have an empty primary constructor and set default values for all my class attributes.
If any given constructor can guarantee all the fields in the object are initialized, why must there be a primary constructor? In other words, why can we not have multiple primary constructors?