Prev: Call for papers: AIPR-10, Orlando, USA
Next: Call for Paper The International Journal of Computer Science (IJCS)
From: piscesboy on 21 Feb 2010 05:33 I have been programming CLOS for exactly one week now and I'm still a bit fuzzy in my understanding of the syntax of method inheritance. You can write methods that act on specific types of classes, but how do you do then call a method that acts on a class that the specific class inherits from? In other words, how does a more specific method call a less specific method for acting on a class that inherits from another class in the same method? I can write numerous methods (method-1 (classA)) (method-2 (classB)) etc, where classA inherits from classB and but is there a way to combine them in one method where I can call method-2 for classB from within method-1 for classA, where I can pass classA as a parameter? Basically, I know how to write methods that share the same symbol but take different parameters to handle the case of different classes, but there has to be a more efficient way to call these methods from within the most specific method and save typing, right? So, I define a class foo that inherits from bar, right? (defclass bar () ((name :initarg name :accessor name) (telephone :initarg :telephone :accessor telephone))) (defclass foo (bar) ((social-security-num :initarg :SSN :accessor SSN))) (defmethod print-info ((f foo)) (print "Stuff for foo")) (defmethod print-info ((b bar)) <-- How do I call from inside the foo function? (print "Stuff for bar")) How can I call the next most specific method for bar from within the method for foo? I feel like this should be intuitive especially coming from Java, but I can't get it to work quite right.
From: knobo on 21 Feb 2010 06:14 On Feb 21, 11:33 am, piscesboy <oraclmas...(a)gmail.com> wrote: <-cut-> > How can I call the next most specific method for bar from within the > method for foo? I feel like this should be intuitive especially coming > from Java, but I can't get it to work quite right. (defmethod print-info ((b foo)) (print "Stuff for foo") (call-next-method))
From: joswig on 21 Feb 2010 07:24
On 21 Feb., 11:33, piscesboy <oraclmas...(a)gmail.com> wrote: > I have been programming CLOS for exactly one week now and I'm still a > bit fuzzy in my understanding of the syntax of method inheritance. > > You can write methods that act on specific types of classes, but how > do you do then call a method that acts on a class that the specific > class inherits from? In other words, how does a more specific method > call a less specific method for acting on a class that inherits from > another class in the same method? > > I can write numerous methods (method-1 (classA)) (method-2 (classB)) > etc, where classA inherits from classB and but is there a way to > combine them in one method where I can call method-2 for classB from > within method-1 for classA, where I can pass classA as a parameter? > > Basically, I know how to write methods that share the same symbol but > take different parameters to handle the case of different classes, but > there has to be a more efficient way to call these methods from within > the most specific method and save typing, right? > > So, I define a class foo that inherits from bar, right? > > (defclass bar () > ((name :initarg name :accessor name) > (telephone :initarg :telephone :accessor telephone))) > > (defclass foo (bar) > ((social-security-num :initarg :SSN :accessor SSN))) > > (defmethod print-info ((f foo)) > (print "Stuff for foo")) > > (defmethod print-info ((b bar)) <-- How do I call from inside the foo > function? > (print "Stuff for bar")) > > How can I call the next most specific method for bar from within the > method for foo? I feel like this should be intuitive especially coming > from Java, but I can't get it to work quite right. When you call PRINT-INFO, then Common Lisp will use the most specific primary method. To call the next method use (CALL-NEXT-METHOD), that will call the next most specific method. By default only the most specific primary method will be called and you have to use CALL-NEXT-METHOD. There are three other standard method types: :before, :after, :around . You should read in some CLOS tutorial what they do, but basically all :before and all :after methods are automatically called in a specified order. (defmethod print-info :before ((f foo)) (print "now preparing to print some info")) The :before methods will be called first, then the most specific primary method and then all the :after methods. |