Re: Common Lisp for Java developers tutorial?



On Sat, 29 Dec 2007 21:01:10 +0100, Victor Anyakin <victor@xxxxxxxxxxxxxxxxxxxxx> wrote:
Dan Bensen <randomgeek@xxxxxxxxxxxxxx> writes:

Consider other examples, like constructors. There is virtually a
single way to implement a constructor, in CLOS there are two (as far
as I've learned till now): using INITIALIZE-INSTANCE :after method, or
by using a custom function like MAKE-ARTICLE which will take all the
required parameters, process them and instantiate an object as needed.

You can also do initialization stuff in the :initforms of slots or in the :default-initargs. And of course there is :before, :around and :after for initialize-instance and shared-initialize.

Those are far from equivalent; they are executed at different times, initargs/initforms can be overwritten through parameters for make-instance, initialize-instance :before/:after is executed for subclasses, even if they have their own specialized methods (whereas initforms/initargs are overwritten) etc.

First, decide what you want to do and second, choose the right tool.

There are other things a Java developer coming to the Common Lisp
world might want to know. As far as I've got it till now, CLOS is not
really the "must" thing, since there are other ways to live without
classes in Common Lisp. And that point might be also worth explaining
for the newbies...

Consider the following:

(defun make-box (width length)
(let ((w width) (l length))
(list
:area #'(lambda () (* w l))
:set-dimmensions #'(lambda (width length) (setf w width l length))
:get-dimmensions #'(lambda () (list w l)))))

Create "instances" with

(defparameter *box* (make-box 2 3))

And call "methods" with

(funcall (getf *box* :area))
==> 6

Change a "member"

(funcall (getf *box* :set-dimmensions) 4 5)

And check:

(funcall (getf *box* :area))
==> 20

That is, using closures you can imitate the behavior of classes without any OO overhead. The example above could have been implemented just as well using defclass and defmethod -- more a matter of taste than anything else. If you have other situations things might look differently; for instance if you need a large number of instances, and only one "class method", it might be preferable to store closures in a hash table.

HTH,
Peter


However, thank you all for helping to find valuable sources of
information,

With best regards,

Happy New Year!

Victor



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
.