Invokedynamic and Multiple Dispatch
John.Rose at Sun.COM
Fri Mar 5 22:51:16 PST 2010
On Feb 28, 2010, at 3:48 PM, Larry Chester wrote:
> So I've been playing around with invokedynamic (with build 84),
> however I've stumbled on something I find quite strange!
> Consider two objects a and b that both reference a String. a is
> declared as a String but b is only an Object. When I dynamically
> invoke with either a or b as an argument I would expect the MethodType
> passed to the bootstrap method to be identical.
> String a = "hello";
> Object b = a;
> However, when called with a, the MethodType is (java.lang.String)void;
> but with b, it is (java.lang.Object)void.
How could it be otherwise? The static type of b is Object, while the static type of a is String. If they happen to have the same dynamic type, that has nothing to do with the semantics of bytecoded call sites.
> I've dug around all over the place but can't find anything that
> confirms what the behaviour should be in this case. I was hoping
> invokedynamic could be used for multiple dispatch but that doesn't
> appear possible.
> I'm interested what the intended functionality is in this case. Is
> that a bug or intended?
Totally intended. How does this hinder multiple dispatch? In fact, the extra information about static type, which is distinct from the dynamic types of the actual arguments, gives you the information you need to accurately model Java's static overload resolution rules.
If you are modeling some other form of multiple dispatch (there are many ways to do it) you can just ignore the static types and convert everything to object. Note that MethodHandles.convertArguments gives you a way to adapt your own target method (which might be (Object)->Object) to the required call site target (which might be (String)->void). The JVM will insert any casts needed to make up the type differences.
More information about the mlvm-dev