The docs says that `dynamic` is not supported by t...
# compiler
t
The docs says that
dynamic
is not supported by the JVM backend. Would it be acceptable to have it replaced by an IR plugin? I would use it in the context of data serialization:
Any
would mean polymorphic serialization,
dynamic
would mean "there is something but we don't really know what". As I understand the meaning of
dynamic
it is exactly this. In this example
signature
describes what's in
default
.
Copy code
@Adat
class AdaptiveTemplateVariable(
    val name: String,
    val index: Int,
    val signature: String,
    val default : dynamic
)
j
Dynamic lets you call arbitrary functions without them being defined, use arbitrary properties without them being defined, and instances can be used as any type. That's simply not possible on the JVM because bytecode requires explicit signatures for invocation and use as type instances
a
That's simply not possible on the JVM
doesn't Groovy demonstrate it is possible?
j
Are you referring to the use of reflection? You still cannot dispatch to a method without knowing the types of its parameters, and if you're going to guess at overloads you're still moving the burden of resolving the overload to the caller rather than the receiver such as in JS. It's also insanely slow, unlike
dynamic
which emits regular JS.
t
I don't want JVM to handle
dynamic
. I want the source code to contain
dynamic
and the replace it with an IR plugin. My question is: does this goes agains some rule?
a
maybe someone else can answer about the rules, but I do like the idea of a dynamic on JVM. However, I think you might be asking for something else. Is the
dynamic
you're proposing similar to a generic type? Or an expect/actual? Would the possible values of
dynamic
be limited to a subset of types?
I guess you want something like this?
Copy code
@Adat
class AdaptiveTemplateVariable(
    val signature: String,
    val default : dynamic
)

fun main() {
  val v = AdaptiveTemplateVariable(signature = "kotlin.String", default = "x")
}
t
More or less. Actually I would rarely, if ever, use the constructor manually. This is meant to be part of a templating system which would automatically encode/decode the values. Technically I could just use ByteArray but that would look like strange in Json.