So I guess I'm just wondering what is the right ap...
# announcements
v
So I guess I'm just wondering what is the right approach here
j
when in doubt change that <*> to <Any?> and see if it says someething different. <*> is <out Any?> not to be confused with writable <Any?>
i just did a double take, you may be declaring <Any?> by using <*> upstream of attempting to use <Any>
they mix like oil and water.
m
I think what you may need is the Typesafe Heterogeneous Container pattern, explained in Effective Java. Also known as TypeMap or SymbolMap: https://gerardnico.com/code/design_pattern/typesafe_heterogeneous_container If you’re writing kotlin-only code I suggest you manipulate
KClass
instead of Java
Class
. Here’s an example implementation in pure Kotlin
j
i entertained KClass at one point and couldn't find the static instances forName, instanceOf, etc. maybe it's there but it seemed easier to just use Class<>
m
maybe seems easier coming from Java, but when you need to code only in Kotlin it gets annoying to use
.java
or
.javaClass
all the time. Moreover KClass can provide you with all sort of K-specific introspection (as well as the
isInstance
function), just take a look at its api: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-class/index.html As for dynamic loading on the JVM, use the plain old java method and an extension:
Copy code
val kClass = Class.forName("mypackage.MyClass").kotlin