www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Anonymous nested classes

reply "John C" <johnch_atms hotmail.com> writes:
I'm trying out the new anonymous nested classes, and can't figure out why 
this won't compile:

    public interface Iterator {
      public bool moveNext();
      public Object current();
    }

    public class Stack {

      public Iterator interator() {
        return new class Iterator {

          public this() {
            currentIndex_ = items_.length - 1;
            // error here: this for items_ needs to be type Stack not type 
void*
          }

          public bool moveNext() {
            return currentIndex_ >= 0;
          }

          public Object current() {
            return items_[currentIndex_];
            // error here: this for items_ needs to be type Stack not type 
void*
          }

          private int currentIndex_;

        };
      }

      private Object[] items_;

    }

The anonymous class (which inherits from Iterator) should have access to its 
enclosing class's members. Or have I gone bonkers? 
Jun 09 2005
next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
         return new class Iterator {
I think this is supposed to be something like:
           public this() {
             currentIndex_ = items_.length - 1;
             // error here: this for items_ needs to be type Stack not type
void*
           }
I have no clue... Probably something odd about the way anonymous classes are implemented. Might try using something like 'Stack.items_' but I would expect that to be treated as a static referance. Maybe 'this.outer.items_' or something therelike will become available. -- Chris Sauls
Jun 09 2005
parent "John C" <johnch_atms hotmail.com> writes:
"Chris Sauls" <ibisbasenji gmail.com> wrote in message 
news:d8a67b$264i$1 digitaldaemon.com...
         return new class Iterator {
I think this is supposed to be something like:
           public this() {
             currentIndex_ = items_.length - 1;
             // error here: this for items_ needs to be type Stack not 
 type void*
           }
I have no clue... Probably something odd about the way anonymous classes are implemented. Might try using something like 'Stack.items_' but I would expect that to be treated as a static referance. Maybe 'this.outer.items_' or something therelike will become available. -- Chris Sauls
Doesn't work, sadly. Since the point was to ease translation of Java code, I thought returning anonymous classes would be possible, as it is in Java. Looks like it's not. Has anyone else sussed this out yet? Perhaps an example in the spec is in order.
Jun 09 2005
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
It's a compiler bug. I need to get it fixed.
Jun 10 2005
next sibling parent Stefan <Stefan_member pathlink.com> writes:
In article <d8d2gp$20ea$1 digitaldaemon.com>, Walter says...
It's a compiler bug. I need to get it fixed.
Sorry, I don't see how DMD 0.127 really fixes that, it still errs (now complaining, that items_ is not accessible). How to make that code compilable (other than making items_ public). Sorry if this is a dumb question, i'm a D newbie ;) Kind regards, Stefan
Jun 23 2005
prev sibling parent reply Stefan <Stefan_member pathlink.com> writes:
In article <d8d2gp$20ea$1 digitaldaemon.com>, Walter says...
It's a compiler bug. I need to get it fixed.
Sorry, I don't see how DMD 0.127 really fixes that, it still errs (now complaining, that items_ is not accessible). How to make that code compilable (other than making items_ public). Sorry if this is a dumb question, i'm a D newbie ;) Kind regards, Stefan
Jun 23 2005
parent Stefan <Stefan_member pathlink.com> writes:
In article <d9fce8$gcs$1 digitaldaemon.com>, Stefan says...
In article <d8d2gp$20ea$1 digitaldaemon.com>, Walter says...
It's a compiler bug. I need to get it fixed.
Sorry, I don't see how DMD 0.127 really fixes that, it still errs (now complaining, that items_ is not accessible). How to make that code compilable (other than making items_ public). Sorry if this is a dumb question, i'm a D newbie ;) Kind regards, Stefan
Sorry for double posting. Seem to have hitten the post button twice :-(
Jun 23 2005