From: Stefan Arentz
Subject: Java inline classes in lisp/clos
Date: 
Message-ID: <87odt4nxry.fsf@kip.sateh.com>
I'm a lisp newbie so forgive me for asking silly questions. Suppose I have
the following Java code:

 public class IndexPage extends WebPage
 {
    public WebPage()
    {
        super();

        add(new Label("title", "This is the title of the page"));

        add(new Link("testLink")
        {
            public void onClick()
            {
                // Do something when the link is clicked
            }
        });
    }
 }

I'm mostly interested in the abstract Link#onClick which is inlined.

How would I translate that to lisp? What would that style of programming
look like in lisp or more specifically, clos.

 S.

From: Javier
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <1159189469.160405.17980@i42g2000cwa.googlegroups.com>
Stefan Arentz ha escrito:

>  public class IndexPage extends WebPage
>  {
>     public WebPage()
>     {
>         super();
>
>         add(new Label("title", "This is the title of the page"));
>
>         add(new Link("testLink")
>         {
>             public void onClick()
>             {
>                 // Do something when the link is clicked
>             }
>         });
>     }
>  }
>


What about something like this:

-----------------------------------------
(defclass web-page ()
  ((components :initarg :components
	      :initform nil)))

(defclass component ()
  ((events :initform (make-hash-table)
	  :initarg :events)))

(defclass label (component)
  ((title :initarg :title)))

(defclass link (component)
  ((to :initarg :to)))

(defclass index-page (web-page) ())

(defmethod initialize-instance :after ((page index-page) &key)
  (push (make-instance 'label :title "This is the title of the page")
	(slot-value page 'components))
  (with-slots (events) (make-instance 'link :to "any place")
    (setf (gethash 'on-click events)
	  #'(lambda ()
	      #|here goes the code for on-click|#))))
----------------------------------------------

I hope so more coments from peope here.
From: Javier
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <1159190833.248541.209910@e3g2000cwe.googlegroups.com>
Javier ha escrito:

> What about something like this:

Sorry there is a little bug (the event is not added), here is a revised
version:

(defclass web-page ()
  ((components :initarg :components
	      :initform nil)))

(defclass component ()
  ((events :initform (make-hash-table)
	  :initarg :events)))

(defmethod add-event ((comp component) event)
  (push event (gethash 'events (slot-value comp 'events)))
  comp)

(defclass label (component)
  ((title :initarg :title)))

(defclass link (component)
  ((to :initarg :to)))

(defclass index-page (web-page) ())

(defmethod initialize-instance :after ((page index-page) &key)
  (push (make-instance 'label :title "This is the title of the page")
	(slot-value page 'components))
  (push (add-event (make-instance 'link :to "any place")
		   #'(lambda ()
		       #|here goes the code for on-click|#))
	(slot-value page 'components)))
From: Javier
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <1159191357.278747.241420@k70g2000cwa.googlegroups.com>
Javier ha escrito:

> Javier ha escrito:
>
> > What about something like this:
>
> Sorry there is a little bug (the event is not added), here is a revised
> version:

And there is still another bug. Hope you (or other) can discover it.
Ttake it as an exercise. :-)
From: Pascal Costanza
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <4nvu5gFc66srU1@individual.net>
Javier wrote:
> Javier ha escrito:
> 
>> What about something like this:
> 
> Sorry there is a little bug (the event is not added), here is a revised
> version:
> 
> (defclass web-page ()
>   ((components :initarg :components
> 	      :initform nil)))
> 
> (defclass component ()
>   ((events :initform (make-hash-table)
> 	  :initarg :events)))
> 
> (defmethod add-event ((comp component) event)
>   (push event (gethash 'events (slot-value comp 'events)))
>   comp)
> 
> (defclass label (component)
>   ((title :initarg :title)))
> 
> (defclass link (component)
>   ((to :initarg :to)))
> 
> (defclass index-page (web-page) ())
> 
> (defmethod initialize-instance :after ((page index-page) &key)
>   (push (make-instance 'label :title "This is the title of the page")
> 	(slot-value page 'components))
>   (push (add-event (make-instance 'link :to "any place")
> 		   #'(lambda ()
> 		       #|here goes the code for on-click|#))
> 	(slot-value page 'components)))

This resembles the original Java code, but is probably not what you 
would write in Common Lisp.

For example, it seems to me that specializing initialize-instance in 
CLOS is much rarer than writing constructors in Java. Say, the class 
index-page could roughly look like this:

(defclass index-page (web-page)
   ((components :initform (list (make-instance ...) ...))))

...but even that is weird.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Javier
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <1159391635.425531.225690@h48g2000cwc.googlegroups.com>
Pascal Costanza ha escrito:

> This resembles the original Java code, but is probably not what you
> would write in Common Lisp.
>
> For example, it seems to me that specializing initialize-instance in
> CLOS is much rarer than writing constructors in Java. Say, the class
> index-page could roughly look like this:
>
> (defclass index-page (web-page)
>    ((components :initform (list (make-instance ...) ...))))
>
> ...but even that is weird.

Sure, this is my fault, as I'm still very C++/Java oriented. Can you
please present some example code of how would you do it?
From: Pascal Costanza
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <4nplfsFbh049U1@individual.net>
Stefan Arentz wrote:
> I'm a lisp newbie so forgive me for asking silly questions. Suppose I have
> the following Java code:
> 
>  public class IndexPage extends WebPage
>  {
>     public WebPage()
>     {
>         super();
> 
>         add(new Label("title", "This is the title of the page"));
> 
>         add(new Link("testLink")
>         {
>             public void onClick()
>             {
>                 // Do something when the link is clicked
>             }
>         });
>     }
>  }

That's not valid Java code.

> I'm mostly interested in the abstract Link#onClick which is inlined.
> 
> How would I translate that to lisp? What would that style of programming
> look like in lisp or more specifically, clos.

Inner classes don't exist in CLOS, at least not in the Java sense. But 
you can use anonymous functions, aka closures, that can be created with 
lambda expressions.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Stefan Arentz
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <87k63snvw4.fsf@kip.sateh.com>
Pascal Costanza <··@p-cos.net> writes:

> Stefan Arentz wrote:
> > I'm a lisp newbie so forgive me for asking silly questions. Suppose I have
> > the following Java code:
> >  public class IndexPage extends WebPage
> >  {
> >     public WebPage()
> >     {
> >         super();
> >         add(new Label("title", "This is the title of the page"));
> >         add(new Link("testLink")
> >         {
> >             public void onClick()
> >             {
> >                 // Do something when the link is clicked
> >             }
> >         });
> >     }
> >  }
> 
> That's not valid Java code.

Except for the typo in the constructor name (should be IndexPage) it is
perfectly legal.

> > I'm mostly interested in the abstract Link#onClick which is inlined.
> > How would I translate that to lisp? What would that style of
> > programming
> > look like in lisp or more specifically, clos.
> 
> Inner classes don't exist in CLOS, at least not in the Java sense. But
> you can use anonymous functions, aka closures, that can be created
> with lambda expressions.

Would you mind giving an example? I'm trying to understand how the above
code can be implemented in the most elegant way in lisp. Most of lisp and
clos is still magic to me and examples of good style really help >:-)

 S.
From: Pascal Costanza
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <4npmk7Fbg0gkU1@individual.net>
Stefan Arentz wrote:
> Pascal Costanza <··@p-cos.net> writes:
> 
>> Stefan Arentz wrote:
>>> I'm a lisp newbie so forgive me for asking silly questions. Suppose I have
>>> the following Java code:
>>>  public class IndexPage extends WebPage
>>>  {
>>>     public WebPage()
>>>     {
>>>         super();
>>>         add(new Label("title", "This is the title of the page"));
>>>         add(new Link("testLink")
>>>         {
>>>             public void onClick()
>>>             {
>>>                 // Do something when the link is clicked
>>>             }
>>>         });
>>>     }
>>>  }
>> That's not valid Java code.
> 
> Except for the typo in the constructor name (should be IndexPage) it is
> perfectly legal.

Unless something very fundamental has changed recently, the definition 
of an anonymous inner class is missing.

>>> I'm mostly interested in the abstract Link#onClick which is inlined.
>>> How would I translate that to lisp? What would that style of
>>> programming
>>> look like in lisp or more specifically, clos.
>> Inner classes don't exist in CLOS, at least not in the Java sense. But
>> you can use anonymous functions, aka closures, that can be created
>> with lambda expressions.
> 
> Would you mind giving an example? I'm trying to understand how the above
> code can be implemented in the most elegant way in lisp. Most of lisp and
> clos is still magic to me and examples of good style really help >:-)

That doesn't make a lot of sense because the code in Lisp would look 
very different. It's better to first learn about functions as 
first-class values, anonymous functions and higher-order functions, as 
they are presented in every good Lisp tutorial. Then it becomes clear 
how you would do this.


Very roughly, it would look something like this:

(add "testLink" (lambda () #|do something on click|#))

But I guess this doesn't really help.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: verec
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <451845e2$0$627$5a6aecb4@news.aaisp.net.uk>
On 2006-09-25 10:42:40 +0100, Pascal Costanza <··@p-cos.net> said:

>>>> I'm a lisp newbie so forgive me for asking silly questions. Suppose I have
>>>> the following Java code:
>>>>  public class IndexPage extends WebPage
>>>>  {
>>>>     public WebPage()
>>>>     {
>>>>         super();
>>>>         add(new Label("title", "This is the title of the page"));
>>>>         add(new Link("testLink")
>>>>         {
>>>>             public void onClick()
>>>>             {
>>>>                 // Do something when the link is clicked
>>>>             }
>>>>         });
>>>>     }
>>>>  }
>>> That's not valid Java code.
>> 
>> Except for the typo in the constructor name (should be IndexPage) it is
>> perfectly legal.
> 
> Unless something very fundamental has changed recently, the definition 
> of an anonymous inner class is missing.

No, it is not.

A better indentation reveals the truth :-)

            add(
                new Link("testLink") {
                    public void onClick() {
                        // Do something when the link is clicked
                    }
                }
            ) ;

If you prepend top definitions:

class WebPage {
    public void add(Link l) {}
    public void add(Label l) {}
}

class Label {
    Label(String s1, String s2) {}
}

class Link {
    Link(String s1) {}
}

and introduce a main:

        // ....
        add(
            new Link("testLink") {
                public void onClick() {
                    // Do something when the link is clicked
                }
            }
        ) ;
    }

    public static void main(String[] args) {
        new IndexPage() ;
    }
 }

it not only compiles, but also runs (and, expectedly, produces
no output).
From: Pascal Costanza
Subject: Re: Java inline classes in lisp/clos
Date: 
Message-ID: <4nr005Fabq5iU1@individual.net>
verec wrote:
> On 2006-09-25 10:42:40 +0100, Pascal Costanza <··@p-cos.net> said:
> 
>>>>> I'm a lisp newbie so forgive me for asking silly questions. Suppose 
>>>>> I have
>>>>> the following Java code:
>>>>>  public class IndexPage extends WebPage
>>>>>  {
>>>>>     public WebPage()
>>>>>     {
>>>>>         super();
>>>>>         add(new Label("title", "This is the title of the page"));
>>>>>         add(new Link("testLink")
>>>>>         {
>>>>>             public void onClick()
>>>>>             {
>>>>>                 // Do something when the link is clicked
>>>>>             }
>>>>>         });
>>>>>     }
>>>>>  }
>>>> That's not valid Java code.
>>>
>>> Except for the typo in the constructor name (should be IndexPage) it is
>>> perfectly legal.
>>
>> Unless something very fundamental has changed recently, the definition 
>> of an anonymous inner class is missing.
> 
> No, it is not.
> 
> A better indentation reveals the truth :-)
> 
>            add(
>                new Link("testLink") {
>                    public void onClick() {
>                        // Do something when the link is clicked
>                    }
>                }
>            ) ;

Argh! :)

OK.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/