Warning: I'm new to Lisp, CLOS, and FP.
What in CLOS is equivalent to abstract and interface classes in C++? In
other words, how can I promise other parts of my code that, if they can
make an instance of a particular class, there are methods specialized for
that class? For instance, in C++:
class Shape {
public:
virtual void draw () = 0; // Draw the shape on the screen
};
If I instantiate an object of any class derived from Shape, I know that
there is some implementation of a draw() method that can draw it.
Moreover, unless a class derived Shape implements draw(), that class
cannot be instantiated.
Any help or pointers to info is appreciated.
--
===============================
Jeffrey S. Sharp (XorAxAx)
(jss at subatomix dot com)
-----BEGIN GEEK CODE BLOCK-----
Version 3.12
GCS/IT/MU ··@ s-:+ a20 C++(++++) UBL+(+++$)> P L+(+++$)>
!E W++ N+(++) o? K? w++$> !O M(-) !V PS+ PE Y PGP- t+
5 X+ R(+) tv+ b+ DI++(+++) G++ e> h--- r+++ y+++
------END GEEK CODE BLOCK------
From: Chris Double
Subject: Re: Q: Ensuring Presence of Method Implementation? (Newbie)
Date:
Message-ID: <wkog9g5d4b.fsf@double.co.nz>
"Jeffrey S. Sharp" <···@NON.subatomix.SPAM.com> writes:
> What in CLOS is equivalent to abstract and interface classes in C++?
I don't know an easy answer for you, but you might want to take a look
at some of the answers in this deja news thread from back in Jan 1999:
http://www.deja.com/thread/433647082
There was quite a bit of discussion on this very topic.
Chris.
From: Marco Antoniotti
Subject: Re: Q: Ensuring Presence of Method Implementation? (Newbie)
Date:
Message-ID: <lw1z66oocf.fsf@parades.rm.cnr.it>
"Jeffrey S. Sharp" <···@NON.subatomix.SPAM.com> writes:
> Warning: I'm new to Lisp, CLOS, and FP.
>
> What in CLOS is equivalent to abstract and interface classes in C++? In
> other words, how can I promise other parts of my code that, if they can
> make an instance of a particular class, there are methods specialized for
> that class? For instance, in C++:
>
> class Shape {
> public:
> virtual void draw () = 0; // Draw the shape on the screen
> };
A not so direct way to represent what you just wrote is:
(defmethod draw ((x t)) (error "Unimplemented DRAW method."))
It is up to you to check whether a method is implemented or not. OTHO,
in CL you have ways to check at run time whether a method is
implemented or not. Try that with C++.
> If I instantiate an object of any class derived from Shape, I know that
> there is some implementation of a draw() method that can draw it.
> Moreover, unless a class derived Shape implements draw(), that class
> cannot be instantiated.
Methods are not associated with classes in CLOS (which is a good
thing, given that you can have multi-methods thanks to this design;
antoher thign you may want to try to do in C++ :) ).
Therefore, classes can always be instantiated, unless you specify a
special INITIALIZE-INSTANCE or SHARED-INITIALIZED which signals an
error. You may like it or not, but that is the way it is.
Methods, OTOH, are applicable to instances/classes combinations,
therefore you need to specify them "on the side". Encapsulation is
taken care of by CL package machinery.
Cheers
--
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - 06 68 10 03 17, fax. +39 - 06 68 80 79 26
http://www.parades.rm.cnr.it/~marcoxa