Constructor Generic Type Inference
- From: rossum <rossum48@xxxxxxxxxxxx>
- Date: Fri, 20 Jun 2008 19:31:19 +0100
Most programmers seem to agree that it is tedious to have to type long
generic type parameters twice, when the second copy is effectively
redundant:
MyGenericClass<String, Map<Integer, String>> myThing =
new MyGenericClass<String, Map<Integer, String>>();
Various suggestions have been made to add some form of type inference
to constructors in order to reduce typing. So far Sun seems to have
rejected all of them.
Here is another one. :)
I suggest using angle brackets with ellipsis, "<...>", to indicate to
the compiler that it should use exactly the same generic parameters as
on the left side of "= new" in this location on the right hand side:
MyGenericClass<String, Map<Integer, String>> thing =
new MyGenericClass<...>();
Using ellipsis like this saves typing. It seems to me to be easily
understandable by someone reading the code as the referent of the
ellipsis is present on the left hand side of the assignment. In order
not to separate the ellipsis from its referent, the left hand side of
the assignment would have to include the generic parameters, so code
like:
MyGenericClass<String, Map<Integer, String>> myThing;
// 0 or more lines of code omitted
myThing = new MyGenericClass<...> myThing();
should fail to compile, because it should be easy for someone reading
the code to understand what the <...> is referring to. Hence the
"<...>" must only be allowed to refer to the left side of the
assignment it is placed in rather than to the original declaration,
which might be pages away in the code listing.
The advantages I can see for this are:
- it is easier to type
- it is easily readable and understandable
- it limits constructor type inference to the places it is required
and nowhere else
- it should be easy to implement
- it is under the control of the programmer; if type inference is not
wanted in a particular situation then the existing format can be used
instead of the proposed new format
- if required it could be extended to other similar situations while
retaining control and readability
Thoughts?
rossum
.
- Follow-Ups:
- Re: Constructor Generic Type Inference
- From: Hendrik Maryns
- Re: Constructor Generic Type Inference
- From: Lew
- Re: Constructor Generic Type Inference
- From: Roedy Green
- Re: Constructor Generic Type Inference
- Prev by Date: Re: Why does Java not have a RealNumber class?
- Next by Date: Re: Constructor Generic Type Inference
- Previous by thread: Media Framework presence
- Next by thread: Re: Constructor Generic Type Inference
- Index(es):
Relevant Pages
|
Loading