does this work with statically typed languages? I ...
# announcements
r
does this work with statically typed languages? I know Scala has something like
Dynamic
but this is not what you mean probably
g
Hum, we have a dynamic thing in kotlin, but only for js, I think. What I would like to do is to create an api controller that could call any implemented method on a business object of mine. Since I have a lot of possible methods it would be very nice to have a dynamic way to do that.
r
The dynamic type is not supported in code targeting the JVM
in Scala land, if your instance would be of type
Dynamic
then you could call an “unknown” method I believe (the compiler does the magic). But if you use any other type, it would give you compile errors. I haven’t seen the same thing in Kotlin but maybe it exists. I haven’t used Kotlin that long
g
Hum, so, if this is possible in Scala it could be possible in Kotlin too.
j
I don't know of any JVM hooks that would let you intercept on the receiver object after failed method resolution. https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html#jvms-5.4.3.3
m
Well, as far as I know accesses to the
Dynamic
type in Scala compile to calls of the reflection API.
And there are ways to hook into the method resolution strategy on newer JVMs: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/invoke/package-summary.html
j
That's cool, haven't played with Scala - would make sense. You'd absolutely have the opportunity to inspect whether it existed and handle it in that case.
r
>> Scala’s Dynamic type allows you to access instance members that aren’t statically defined in a class. It’s a marker trait with no members - it provides a hint to the compiler to rewrite method calls and field accesses to missing members
dyn.foo //dyn.selectDynamic(“foo”)
so it’s not using reflection but different method calls
m
Hmm, interesting!