Notes on implementing concise calls to constructors with type parameters
Joseph D. Darcy
Joe.Darcy at Sun.COM
Mon Mar 2 23:04:44 PST 2009
Hello.
As implied by previous email on the list, Neal and I met recently and
discussed an implementation strategy for supporting concise calls to
constructors with type parameters using the diamond "<>" notation.
In brief, since the desired behavior to emulate is having the bounds of
constructors determined as if a static generic factory method was used
instead, the compiler could synthesize artificial static methods for the
purposes of method resolution and finding the bounds, but then discard
those synthesized methods and map back to the right constructor.
There are a few hazards to watch out for:
* Generic constructors that declare their own type variables
* accessibility modifiers
* var-args
* boxing/unboxing conversion
The running example was something like
class C<T> {
C(Arg1 arg1)
C(Arg 1 arg1, Arg 2 arg2)
<U> C(Arg 1 arg1)
}
Both T and U could appear in the argument list somewhere; they could
also have bounds. The translation would be to
<S, U> static /* orig. modifiers */ C<S> staticFactory(/* orig. list of
args*/)
where S is just an alpha-rename of T and U would be omitted for
non-generic constructors. Once the appropriate constructor with its
bound is chosen, inference might have to be redone on the constructor
to bind any generic type parameters (and account for var-args and boxing
and unboxing). Any diagnostic messages should be mapped back to the
constructors rather than the synthesized static factories. The
resolution process results in Symbols and only the phases through Attr
and Resolved should need to be modified.
This mapping of static factories to constructors should probably inform
the specification of the feature as well as its implementation, but I
haven't written any specification along those lines.
Regards,
-Joe
More information about the coin-dev
mailing list