Re: Generics: SupressWarnings("unchecked"). Why, and why not injava.util.ArrayList

Discussion in 'Java' started by Andreas Leitgeb, Oct 21, 2009.

  1. phi <> wrote:
    > public class ListImplementation<T> implements generics.List<T> {
    > Object [] data;


    Just guessing... If you made this T[] typed, does the warning then
    disappear? The array itself would not "know" the difference, but
    the Compiler does.

    > public T getFirst() {
    > if (isEmpty()) {
    > return null; }
    > return (T) data[0]; } // <<---- WARNING


    In that case, you then won't need the cast, either, but probably
    it won't hurt.
    Andreas Leitgeb, Oct 21, 2009
    #1
    1. Advertising

  2. Re: Generics: SupressWarnings("unchecked"). Why, and why not in java.util.ArrayList

    Andreas Leitgeb wrote:
    > phi <> wrote:
    >> public class ListImplementation<T> implements generics.List<T> {
    >> Object [] data;

    >
    > Just guessing... If you made this T[] typed, does the warning then
    > disappear? The array itself would not "know" the difference, but
    > the Compiler does.
    >
    >> public T getFirst() {
    >> if (isEmpty()) {
    >> return null; }
    >> return (T) data[0]; } // <<---- WARNING

    >
    > In that case, you then won't need the cast, either, but probably
    > it won't hurt.


    I'd guess that just moves the warning, say to

    T[] data = (T[])new Object[20];

    Because

    T[]data = new T[20];

    is illegal.
    Mike Schilling, Oct 21, 2009
    #2
    1. Advertising

  3. Mike Schilling <> wrote:
    >>> Object [] data;

    >> Just guessing... If you made this T[] typed, ...

    > I'd guess that just moves the warning, say to
    > T[] data = (T[])new Object[20];
    > Because
    > T[]data = new T[20];
    > is illegal.


    Yes, I noticed after reading the other followup :-}

    PS: I haven't tried "new T[...]", myself. I believe, that it
    *could* theoretically work consistently, but thats moot, if
    it isn't allowed in reality.
    Andreas Leitgeb, Oct 21, 2009
    #3
  4. Andreas Leitgeb

    Lew Guest

    Andreas Leitgeb wrote:
    > PS: I haven't tried "new T[...]", myself.  I believe, that it
    >   *could* theoretically work consistently, but thats moot, if
    >   it isn't allowed in reality.
    >


    Five minutes in the JLS uncovers:
    "10.3 Array Creation
    "... An array creation expression specifies the element type, ...
    "It is a compile-time error if the element type is not a reifiable
    type (§4.7)"

    So, no, that expression is not allowed in reality.

    --
    Lew
    Lew, Oct 21, 2009
    #4
  5. Re: Generics: SupressWarnings("unchecked"). Why, and why not in java.util.ArrayList

    Andreas Leitgeb wrote:
    > Mike Schilling <> wrote:
    >>>> Object [] data;
    >>> Just guessing... If you made this T[] typed, ...

    >> I'd guess that just moves the warning, say to
    >> T[] data = (T[])new Object[20];
    >> Because
    >> T[]data = new T[20];
    >> is illegal.

    >
    > Yes, I noticed after reading the other followup :-}
    >
    > PS: I haven't tried "new T[...]", myself. I believe, that it
    > *could* theoretically work consistently, but thats moot, if
    > it isn't allowed in reality.


    It would cause problems, because the following would be legal code:

    T[] arr = new T[20]; // Not legal now, but we're assuming
    Object[] oarr = arr; // Legal in general, since we know that T
    extends Object
    arr[0] = new Object();// compile-time legal, and since the "real"
    row type
    // of the Array is Object, legal
    at run time too
    T t = arr[0]; // Uh-oh.
    Mike Schilling, Oct 22, 2009
    #5
  6. Mike Schilling <> wrote:
    > Andreas Leitgeb wrote:
    >> PS: I haven't tried "new T[...]", myself. I believe, that it
    >> *could* theoretically work consistently, but thats moot, if
    >> it isn't allowed in reality.


    > It would cause problems, because the following would be legal code:
    > T[] arr = new T[20]; // Not legal now, but we're assuming
    > Object[] oarr = arr; // Legal in general, since we know that
    > // T extends Object
    > arr[0] = new Object(); // compile-time legal, and since the "real"
    > // row type of the Array is Object, legal


    You need of course "oarr[0]" for the assignment, but the argument
    is then sound. I admit defeat of that former belief :)

    > at run time too
    > T t = arr[0]; // Uh-oh.
    Andreas Leitgeb, Oct 22, 2009
    #6
  7. Re: Generics: SupressWarnings("unchecked"). Why, and why not in java.util.ArrayList

    Andreas Leitgeb wrote:
    > Mike Schilling <> wrote:
    >> Andreas Leitgeb wrote:
    >>> PS: I haven't tried "new T[...]", myself. I believe, that it
    >>> *could* theoretically work consistently, but thats moot, if
    >>> it isn't allowed in reality.

    >
    >> It would cause problems, because the following would be legal code:
    >> T[] arr = new T[20]; // Not legal now, but we're assuming
    >> Object[] oarr = arr; // Legal in general, since we know that
    >> // T extends Object
    >> arr[0] = new Object(); // compile-time legal, and since the
    >> "real" // row type of the Array is
    >> Object, legal

    >
    > You need of course "oarr[0]" for the assignment,


    Right. It's difficult to test code that's based on a counterfactual
    assumption :)

    > but the argument
    > is then sound. I admit defeat of that former belief :)
    >
    >> at run time too
    >> T t = arr[0]; // Uh-oh.
    Mike Schilling, Oct 22, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,805
    Smokey Grindel
    Dec 2, 2006
  2. xhy_China
    Replies:
    7
    Views:
    376
    RedGrittyBrick
    Dec 23, 2007
  3. RVic
    Replies:
    19
    Views:
    1,383
  4. Mayeul
    Replies:
    2
    Views:
    3,374
  5. Roedy Green
    Replies:
    2
    Views:
    457
    Mike Schilling
    Oct 23, 2009
Loading...

Share This Page