enhanced enums - back from the dead?
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Wed Dec 5 21:26:08 UTC 2018
Yes, the compiler special cases raw types warnings for j.l.Class type
arguments and also when doing instanceof. That is, in this program:
import java.util.List;
class Test {
void test(List<String> ls) {
ls = (List)null;
boolean b = (ls instanceof List);
Class<List> cl = List.class;
}
}
There will be no raw warnings - just an unchecked warning in the fist
assignment.
The proposed plan for enums is to allow another 'special case' for raw
enum types.
Maurizio
But a raw warning will be generated for your Iterator example.
On 05/12/2018 19:51, Kevin Bourrillion wrote:
> Liam reminded me,
>
> 3. the upper bound for class literals, which maybe went without
> saying, but there it is. Also harmless if I am correct that doing
> anything with the result of `newInstance` etc. always results in one
> kind of warning or another.
>
> Sorry, this is pointless to the real question you're asking, I just
> hate saying something wrong without correcting it. :-)
>
>
> On Wed, Dec 5, 2018 at 11:26 AM Kevin Bourrillion <kevinb at google.com
> <mailto:kevinb at google.com>> wrote:
>
> On Wed, Dec 5, 2018 at 8:19 AM Maurizio Cimadamore
> <maurizio.cimadamore at oracle.com
> <mailto:maurizio.cimadamore at oracle.com>> wrote:
>
> * Our proposed treatment for generic enums raises an
> additional, more
> philosophical, question: what are raw types *for* and how
> happy are we
> in seeing more of them (in the form of raw enum types)?
>
>
> I have looked high and low for valid use cases for raw types
> (apart from interacting with legacy code), and only ever found... two.
>
> 1. As the stepping stone to make a forbidden cast:
>
> @SuppressWarnings("unchecked") // safe because runtime type is
> functionally covariant
> Iterator<Object> objects = (Iterator) strings;
>
>
> A common use case for this is to interact with APIs that forgot
> their wlidcards, but there are plenty of other cases that can't be
> "fixed".
>
> 2. As the argument to instanceof. Everyone does it, of course,
> because writing out `instanceof Map<?, ?>` serves no purpose.
>
> Thankfully, they're both harmless... and I believe (?) they were
> even made exempt from "rawtypes" warnings. (If not, I'd opine that
> they should be.)
>
>
> --
> Kevin Bourrillion | Java Librarian | Google,
> Inc. |kevinb at google.com <mailto:kevinb at google.com>
>
>
>
> --
> Kevin Bourrillion | Java Librarian | Google, Inc. |kevinb at google.com
> <mailto:kevinb at google.com>
More information about the amber-spec-observers
mailing list