is there a way to create an instance of class from...
# kotlin-native
i
is there a way to create an instance of class from string? I can see the KClass<T>.createInstance() from empty constructor etc, but cant see a way to create Kclass from string like "org.atata.SomeClass" is there some static method I miss like Class.forName(str:String)?
o
Nope,
Class.forName
is not supported, and is generally considered bad style. Usually, there are other ways to solve same problem.
i
@olonho for example?
o
Depends on the actual problem.
i
@olonho need to create class instance from class qualifiedName
o
No, this is solution, what is the problem?
i
@olonho need to save bunch of signal calculation algorithms(recalculation+ custom drawing on canvas) that user selected at settings, which are represented as classes. Changes must remain after app is killed.
o
Then map of String -> factory object, for example.
i
@olonho thats what I did, but it is not very comfortable to go somewhere to add 1 more class to support when it could add itself with 0 effort. Its sad that there is no way to do this with createInstance
g
when it could add itself with 0 effort.
And with limitation on class implementation, for example you should have default constructor, cannot pass anything else to construcotr, if class doesn’t follow this implicit contract it will crash on runtime, you never can delete/rename this class, make it abstract etc I would prefer proper factory even on JVM than such “0 effort”