Reputation: 4477
Assume that I am implementing a dynamically typed language on top the JVM 7, which supports the invokedynamic
instruction to link methods at runtime.
The dynamically typed language has a function add
that works on integers by adding them and on strings by concatenating them. Now assume that add
is called by a, say, generic list processing method that only knows (at compile-time) that it holds objects, either integers or strings or both.
How can invokedynamic
help me here when compiling the the method to JVM bytecode as it has to dispatch to two different internal functions, namely the actual function that adds integers and the actual function that concatenates strings?
Upvotes: 4
Views: 317
Reputation: 4477
During my research I have also found the following link, which I would like to share:
It is a collection of source code showing how to use JSR 292 to implement usual patterns that you can find in dynamic language runtimes. (Description copied from their page.)
Upvotes: 0
Reputation: 205865
You might also look at these related articles:
Upvotes: 2
Reputation: 81714
In short, invokedynamic
lets you invoke a method with a given signature without knowledge of the class the method belongs to. If your add()
method just takes an Object
(or other common base type) as an argument, then you can have add(Object)
methods in many otherwise unrelated classes, and invokedynamic
will be able to invoke them. As long as the target object has the method, it will be called.
Upvotes: 3