Generifying outer.this

Discussion in 'Java' started by Safalra, Sep 7, 2005.

  1. Safalra

    Safalra Guest

    I'm currently in the process of 'generifying' my old functional list
    classes before putting them on my website (in case someone wants to use
    inefficient functional-style lists instead of efficient
    imperative-style lists...). Everything has worked well, except for one
    line. The 'truncate' method of my LazyList class creates an Iterator
    using an inner class. The inner class accesses its outer class instance
    through LazyList.this (in order to take the 'tail' repeatedly), but the
    compiler gives this bizarre error message:

    safalra\functional\LazyList.java:213: incompatible types
    found : safalra.functional.LazyList<T>
    required: safalra.functional.LazyList<T>

    If I change the inner class variable in which I'm storing the remainder
    of the list (which is initially the outer class instance) to type
    LazyList (rather than LazyList<T>) the compiler doesn't object to the
    assignment, but then of course the 'tail' is no longer of type
    LazyList<T>.

    For the moment I'm working around this by passing 'this' as a parameter
    to the constructor of the inner class, but this is distinctly
    inelegant. Does anybody know what is causing this problem?

    --
    Safalra (Stephen Morley)
    http://www.safalra.com/programming/java/
    Safalra, Sep 7, 2005
    #1
    1. Advertising

  2. Safalra

    Oliver Wong Guest

    "Safalra" <> wrote in message
    news:...
    > I'm currently in the process of 'generifying' my old functional list
    > classes before putting them on my website (in case someone wants to use
    > inefficient functional-style lists instead of efficient
    > imperative-style lists...). Everything has worked well, except for one
    > line. The 'truncate' method of my LazyList class creates an Iterator
    > using an inner class. The inner class accesses its outer class instance
    > through LazyList.this (in order to take the 'tail' repeatedly), but the
    > compiler gives this bizarre error message:
    >
    > safalra\functional\LazyList.java:213: incompatible types
    > found : safalra.functional.LazyList<T>
    > required: safalra.functional.LazyList<T>
    >
    > If I change the inner class variable in which I'm storing the remainder
    > of the list (which is initially the outer class instance) to type
    > LazyList (rather than LazyList<T>) the compiler doesn't object to the
    > assignment, but then of course the 'tail' is no longer of type
    > LazyList<T>.


    Post an SSCCE (http://www.physci.org/codes/sscce.jsp).

    - Oliver
    Oliver Wong, Sep 7, 2005
    #2
    1. Advertising

  3. Safalra

    Safalra Guest

    Oliver Wong wrote:
    > "Safalra" <> wrote:
    > > [snip unclear explanation]

    >
    > Post an SSCCE (http://www.physci.org/codes/sscce.jsp).


    A useful link - I must remember it for use in the HTML newsgroups. (And
    remember not to be so hypocritical...)

    Right, here's a minimal equivalent of the code (I think), with all the
    references to external classes, methods, and constructors removed:

    public class LazyList<T>{
    private class TruncatedIterator<T>{
    //private LazyList list;
    private LazyList<T> list;
    public TruncatedIterator(LazyList<T> instance){
    //list=instance;
    list=LazyList.this;
    }
    }
    }

    (The 'instance' parameter isn't necessary unless you remove the // from
    the line below.)

    Which gives this error:

    LazyList.java:8: incompatible types
    found : LazyList<T>
    required: LazyList<T>

    If either of the commented-out lines are used instead of the lines
    below, the error magically disappears. It's the fact the removing the
    <T> (first commented-out line) works that makes me think this is some
    generics-related foolishness.

    --
    Safalra (Stephen Morley)
    http://www.safalra.com/programming/java/
    Safalra, Sep 7, 2005
    #3
  4. On Wed, 07 Sep 2005 11:33:40 -0700, Safalra wrote:

    > public class LazyList<T>{
    > private class TruncatedIterator<T>{
    > //private LazyList list;
    > private LazyList<T> list;
    > public TruncatedIterator(LazyList<T> instance){
    > //list=instance;
    > list=LazyList.this;
    > }
    > }
    > }


    You declare a new type parameter (also named T) in the declaration of the
    iterator class. Omit the new type parameter, and things should be just
    peachy.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
    Stefan Schulz, Sep 7, 2005
    #4
  5. Safalra

    Chris Uppal Guest

    Safalra wrote:

    > public class LazyList<T>{
    > private class TruncatedIterator<T>{


    [...]

    > LazyList.java:8: incompatible types
    > found : LazyList<T>
    > required: LazyList<T>


    I believe that this is because you have supplied a generic parameter to
    TruncatedIterator, which hides the outer parameter of the same name.
    TruncatedIterator is -- by implication -- already parameterised on the <T> of
    LazyList, so you don't need to repeat it.

    If you remove the <T> from TruncatedIterator then I think it should work as you
    wish.

    public class LazyList<T>
    {
    private class TruncatedIterator
    {
    private LazyList<T> remainder;

    public TruncatedIterator()
    {
    remainder = LazyList.this;
    }
    }
    }

    -- chris
    Chris Uppal, Sep 7, 2005
    #5
  6. Safalra

    Safalra Guest

    Chris Uppal wrote:
    > Safalra wrote:
    >
    > > public class LazyList<T>{
    > > private class TruncatedIterator<T>{

    >
    > [...]
    >
    > > LazyList.java:8: incompatible types
    > > found : LazyList<T>
    > > required: LazyList<T>

    >
    > I believe that this is because you have supplied a generic parameter to
    > TruncatedIterator, which hides the outer parameter of the same name.
    > TruncatedIterator is -- by implication -- already parameterised on the <T> of
    > LazyList, so you don't need to repeat it.


    Ah, of course! I believe this is the kind of thing my younger brother
    would call a 'schoolboy error'. It does show a lack of usability in the
    compiler however - I suspect many people would make this mistake when
    learning generics, and the baffling error message doesn't help
    diagnosis. Thanks for the assistance.

    --
    Safalra (Stephen Morley)
    http://www.safalra.com/programming/java/
    Safalra, Sep 7, 2005
    #6
  7. Safalra wrote:
    > Chris Uppal wrote:
    >
    >>Safalra wrote:


    [...]

    >>>LazyList.java:8: incompatible types
    >>>found : LazyList<T>
    >>>required: LazyList<T>

    >>
    >>I believe that this is because you have supplied a generic parameter to
    >>TruncatedIterator, which hides the outer parameter of the same name.
    >>TruncatedIterator is -- by implication -- already parameterised on the <T> of
    >>LazyList, so you don't need to repeat it.

    >
    >
    > Ah, of course! I believe this is the kind of thing my younger brother
    > would call a 'schoolboy error'. It does show a lack of usability in the
    > compiler however - I suspect many people would make this mistake when
    > learning generics, and the baffling error message doesn't help
    > diagnosis. Thanks for the assistance.


    Yes, I suspect that many people learning generics (and even some have
    have a fairly good handle on them) do hit something like this. I have
    done. I agree that the error message is a bit baffling, but once you
    already know what it means it's quite clear in hindsight: either the two
    "LazyList" types must be different or the two "T" types must be different.

    --
    John Bollinger
    John C. Bollinger, Sep 8, 2005
    #7
  8. John C. Bollinger wrote:
    > Safalra wrote:
    >
    >> Chris Uppal wrote:
    >>
    >>> Safalra wrote:

    >
    >
    > [...]
    >
    >>>> LazyList.java:8: incompatible types
    >>>> found : LazyList<T>
    >>>> required: LazyList<T>
    >>>
    >>>
    >>> I believe that this is because you have supplied a generic parameter to
    >>> TruncatedIterator, which hides the outer parameter of the same name.
    >>> TruncatedIterator is -- by implication -- already parameterised on
    >>> the <T> of
    >>> LazyList, so you don't need to repeat it.

    >>
    >>
    >>
    >> Ah, of course! I believe this is the kind of thing my younger brother
    >> would call a 'schoolboy error'. It does show a lack of usability in the
    >> compiler however - I suspect many people would make this mistake when
    >> learning generics, and the baffling error message doesn't help
    >> diagnosis. Thanks for the assistance.

    >
    >
    > Yes, I suspect that many people learning generics (and even some have
    > have a fairly good handle on them) do hit something like this. I have
    > done. I agree that the error message is a bit baffling, but once you
    > already know what it means it's quite clear in hindsight: either the two
    > "LazyList" types must be different or the two "T" types must be different.
    >


    "[O]nce you already know what it means it's quite clear" is not really
    the standard that error messages, or any message really, should aim for.

    Ray

    --
    XML is the programmer's duct tape.
    Raymond DeCampo, Sep 8, 2005
    #8
  9. Raymond DeCampo wrote:
    > "[O]nce you already know what it means it's quite clear" is not really
    > the standard that error messages, or any message really, should aim for.


    No, it isn't. I never suggested that it was.

    --
    John Bollinger
    John C. Bollinger, Sep 9, 2005
    #9
  10. Safalra

    Safalra Guest

    Raymond DeCampo wrote:
    > John C. Bollinger wrote:
    > > Safalra wrote:
    > >> Chris Uppal wrote:
    > >>> Safalra wrote:

    > > [...]
    > >>>> LazyList.java:8: incompatible types
    > >>>> found : LazyList<T>
    > >>>> required: LazyList<T>
    > >>>
    > >>> I believe that this is because you have supplied a generic parameter to
    > >>> TruncatedIterator, which hides the outer parameter of the same name.
    > >>> TruncatedIterator is -- by implication -- already parameterised on
    > >>> the <T> of LazyList, so you don't need to repeat it.
    > >>
    > >> Ah, of course! I believe this is the kind of thing my younger brother
    > >> would call a 'schoolboy error'. It does show a lack of usability in the
    > >> compiler however - I suspect many people would make this mistake when
    > >> learning generics, and the baffling error message doesn't help
    > >> diagnosis. Thanks for the assistance.

    > >
    > > Yes, I suspect that many people learning generics (and even some have
    > > have a fairly good handle on them) do hit something like this. I have
    > > done. I agree that the error message is a bit baffling, but once you
    > > already know what it means it's quite clear in hindsight: either the two
    > > "LazyList" types must be different or the two "T" types must be different.

    >
    > "[O]nce you already know what it means it's quite clear" is not really
    > the standard that error messages, or any message really, should aim for.


    Surely it wouldn't be too difficult for the compiler to be changed to
    instead give something like this:

    LazyList.java:8: incompatible types
    found : LazyList<T(1)>
    required: LazyList<T(2)>

    ....where different types, T, with the same name visible to users are
    numbered T(1), T(2), and so on.

    --
    Safalra (Stephen Morley)
    http://www.safalra.com/programming/java/
    Safalra, Sep 10, 2005
    #10
    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. Replies:
    1
    Views:
    15,297
    cbDevelopment
    Nov 13, 2005
  2. lonelyplanet999

    Access outer / inner class variables query

    lonelyplanet999, Nov 11, 2003, in forum: Java
    Replies:
    3
    Views:
    5,284
    lonelyplanet999
    Nov 18, 2003
  3. lonelyplanet999
    Replies:
    1
    Views:
    2,192
    VisionSet
    Nov 13, 2003
  4. Josef Garvi

    outer class keyword

    Josef Garvi, May 28, 2004, in forum: Java
    Replies:
    4
    Views:
    13,067
    Tony Morris
    May 29, 2004
  5. Hendrik Maryns
    Replies:
    3
    Views:
    552
    Hendrik Maryns
    Jun 14, 2006
Loading...

Share This Page