How can polymorphism be achieved with JDK7 invokedynamic

Suppose I embed a dynamically typed language on top of the JVM 7 that supports the invokedynamic for binding methods at runtime.

The dynamically typed language has an add function that works with integers, adding them to strings by concatenating them. Suppose now that add is called, for example, by the processing of a general list, which knows (at compile time) that it contains objects, integers or strings, or both.

How can invokedynamic help me here when compiling a method for the JVM bytecode, since it needs to send two different internal functions, namely the actual function that adds integers and the actual function that concatenates strings?

+4
source share
3 answers

In short, invokedynamic allows you to call a method with a given signature without knowing the class to which the method belongs. If your add() method simply accepts Object (or another common base type) as an argument, you can have add(Object) methods in many other unrelated classes, and invokedynamic can call them. As long as the target has a method, it will be called.

+3
source

During my research, I also found the following link that I would like to share:

JSR 292 Cookbook

This is a set of source code showing how to use JSR 292 to implement common patterns that you can find in dynamic language modes. (Description copied from their page.)

0
source

All Articles