[Pharo-dev] Slot class builder: Anonymous class modification

phil at highoctane.be phil at highoctane.be
Sun Nov 24 06:19:47 EST 2013


Like this:

interface Selector {
  boolean end() ;
  Object current() ;
  void next() ;
}

class Sequence {
  private Object[ ] seq ;
  private int next =0 ;
  public Sequence(int size) {
    seq=new Object[size] ;
  }

  public void add(Object o) {
    if (next<seq.length) {
      seq[next] = o ;
      next++ ;
    }
  }

  public Selector getSelector( ) {
    return new Selector( ) {
      private int index=0 ;
      public boolean end() { return index==next ; }
      public Object current() { return seq[index] ; }
      public void next() { index++ ; }
   } ;
  }
}

class TestSequence {
  public static void main(String[ ] arg) {
    Sequence sRef=new Sequence(10) ;
    for (int i=0 ;i<5 ;i++) sRef.add((“string numero “+i)) ;
    Selector select=sRef.getSelector() ;
    while( !select.end()) {
       System.out.println((String) (select.current())) ;
       select.next() ;
    }
  }
}

Or this

button.addActionListener(
  new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    System.out.println(e.getSource()) ; }
  }
 }
}


On Sun, Nov 24, 2013 at 12:11 PM, Tudor Girba <tudor at tudorgirba.com> wrote:

> In Java, Anonymous classes are classes that literally have no explicit
> name. They are used as replacement for closures.
>
> closure = new Closure() {
>    public void do(Object object) {
>       ...
>    }
> }
> closure.do(42);
>
> A side effect of this construct is that the class is only visible from
> within the method in which it is defined. So, "anonymous" as a term
> describes only one facet of the concept.
>
> Cheers,
> Doru
>
>
>
>
> On Sun, Nov 24, 2013 at 10:24 AM, Stéphane Ducasse <
> stephane.ducasse at inria.fr> wrote:
>
>> So I'm confused.
>>
>> what does it means in Java?
>> What is the meaning (I know that anonymous are the one with $ in the name
>> when looking at them from the Java bycode point of view) but beside that?
>>
>>
>>
>>
>> Indeed. Same for listeners etc.
>>
>> Anonymous class has indeed a very clear meaning to me and to any student
>> that has been exposed to Java (which means "a huge lot").
>>
>> Phil
>>
>>
>> On Sun, Nov 24, 2013 at 1:20 AM, Esteban A. Maringolo <
>> emaringolo at gmail.com> wrote:
>>
>>> 2013/11/23 Igor Stasenko <siguctua at gmail.com>:
>>> > On 23 November 2013 21:26, Esteban A. Maringolo <emaringolo at gmail.com>
>>> >> >> imo, a better term to use for it would be 'private class' , because
>>> >> >> anonymous is a bit fuzzy.
>>>
>>> >> > I disagree.  Anonymous classes is the term that has been used for
>>> over
>>> >> > two decades.  It means, literally, a class that has no name because
>>> it is
>>> >> > not in Smalltalk (or in a top-level environment if the dialect has
>>> >> > namespaces).  This is not at all fuzzy.  Private class means
>>> something quite
>>> >> > different, a class that is private to some environment, e.g. a
>>> class nested
>>> >onyl> > within another class as occurs in SmalltalkAgents or Newspeak.
>>> >>
>>> >> +1
>>> >>
>>> >> Anonymous = without name.
>>> >> There's not much to add to it.
>>>
>>> > That's the point. Now what is practical implications of it?
>>> > Think, how far you can go with anonymous versus private class.
>>> >
>>> > If you deny anonymous classes from being private,
>>> > then you'll immediately hit many problems with tools,
>>> > which working with public classes and expecting them to have a name.
>>> > And i don't have to go deep to point on problems: just imagine that
>>> you did
>>> > a change to such 'anonymous' class, now since it is public, all tools
>>> is
>>> > notified about this change, including change logger.. and my question,
>>> what
>>> > you going to log into .changes file in such case?
>>>
>>> I can see your point. But then just say that "Anonymous classes are
>>> private".
>>> Just don't change the name. Anonymous classes are a well know concept
>>> used through piles of literature.
>>> E.g. In Java the compiler creates anonymous classes everytime you
>>> directly instantiate an Interface.
>>>
>>> ej myObject.schedule(new Runnable {...});
>>>
>>> Regards,
>>>
>>>
>>
>>
>
>
> --
> www.tudorgirba.com
>
> "Every thing has its own flow"
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20131124/351a9353/attachment-0002.html>


More information about the Pharo-dev mailing list