Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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

JSR 292 Cookbook

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.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top