Notes on implementing concise calls to constructors with type parameters
Ali Ebrahimi
ali.ebrahimi1781 at gmail.com
Fri May 15 01:57:28 PDT 2009
Am 15.05.2009 00:51, Alex Buckley schrieb:
>* Ulf, you ask why:
*>*
*>* Cell<String> cs = new Cell(1); //unchecked warning
*>* String s = cs.x; //CCE at runtime
*>*
*>* cannot be rejected at compile-time, presumably with an error rather than
*>* a warning on the assignment.
*>*
*>* The answer is migration compatibility:
My answer is that using a transparent diamond operator in *instantiation
of generic* types by jdk7+.
The code
**Cell<String> cs = new Cell(1); *
if* Cell is generic class
then goto GENERIC_CASE
**GENERIC_CASE:
* treated as :
* Cell<String> cs = new Cell<>(1); *
and this treated as:
* Cell<String> cs = new Cell<**String**>(1); *
goto Next:
else // raw type(legacy code)
RAW_CASE:
*Cell<String> cs = new Cell(1); *
Next:
And finally catching compile time error.
I think one of main goals of adding generic support to java language
was catching run time exceptions
in compile time and having robust and safe code.
I don't think the *Cell* class to be a legacy code(row type)in this case.
I think this particular rule can be applicable at least in Object
instantiation case.
if right side is not a generic type use the row type.
Cell<String> cs = new RowLegacySubCell(10);
By diamond operator if we use:
Cell<String> cs = new RowLegacySubCell*<>*(10);
compiler got an error, because RowLegacySubCell is not generic
class(legacy code).
But by my rule(no diamond operator in code) this compiles fine.
because that the RowLegacySubCell class is not generic(legacy code)
treated as row type.
By this rule we decrease the use of row types and not dropping them
totally ones.
One advantage of this rule is that if some day we decided to update
legacy code and generified them.
By recompiling code, in this time the compiler would use the generic
RowLegacySubCellclass and
We would had:
Cell<String> cs = new RowLegacySubCell(10);
treated as if RowLegacySubCell is generic class:
Cell<String> cs = new RowLegacySubCell<>(10);
and this treated as:
Cell<String> cs = new RowLegacySubCell<String>(10);
And finally catching compile time error.
I think we don'nt need to *the second* step:
*Cell<String> cs = new Cell(1); *
if* Cell is generic class
then goto GENERIC_CASE
**GENERIC_CASE:
* treated as :
* Cell<String> cs = new Cell<**String**>(1); *
goto Next:
else // raw type(legacy code)
RAW_CASE:
*Cell<String> cs = new Cell(1); *
Next:
One note: By this rule we don't need to change code for using new
generic versions of legacy classes.
Therefore by this rule we can slowly remove row types from language.
Best Regards
Ali Ebrahimi
More information about the coin-dev
mailing list