Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generifying outer.this

Reply
Thread Tools

Generifying outer.this

 
 
Safalra
Guest
Posts: n/a
 
      09-07-2005
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/

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      09-07-2005

"Safalra" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
 
 
 
Safalra
Guest
Posts: n/a
 
      09-07-2005
Oliver Wong wrote:
> "Safalra" <(E-Mail Removed)> 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/

 
Reply With Quote
 
Stefan Schulz
Guest
Posts: n/a
 
      09-07-2005
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"


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      09-07-2005
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



 
Reply With Quote
 
Safalra
Guest
Posts: n/a
 
      09-07-2005
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/

 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      09-08-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      09-08-2005
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.
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      09-09-2005
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
(E-Mail Removed)
 
Reply With Quote
 
Safalra
Guest
Posts: n/a
 
      09-10-2005
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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficult generics issue (generifying jakarta commons) Hendrik Maryns Java 3 06-14-2006 12:34 PM



Advertisments