Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Dynamic programming language
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Assembling of code at runtime based on the class of instances=== In the next example, the class '''person''' gets a new superclass. The '''print''' method gets redefined such that it assembles several methods into the effective method. The effective method gets assembled based on the class of the argument and the at runtime available and applicable methods. <syntaxhighlight lang="lisp"> ; the class person CL-USER > (defclass person () ((name :initarg :name))) #<STANDARD-CLASS PERSON 4220333E23> ; a person just prints its name CL-USER > (defmethod print-object ((p person) stream) (print-unreadable-object (p stream :type t) (format stream "~a" (slot-value p 'name)))) #<STANDARD-METHOD PRINT-OBJECT NIL (PERSON T) 40200605AB> ; a person instance CL-USER > (defparameter *person-1* (make-instance 'person :name "Eva Luator")) *PERSON-1* ; displaying a person instance CL-USER > *person-1* #<PERSON Eva Luator> ; now redefining the print method to be extensible ; the around method creates the context for the print method and it calls the next method CL-USER > (defmethod print-object :around ((p person) stream) (print-unreadable-object (p stream :type t) (call-next-method))) #<STANDARD-METHOD PRINT-OBJECT (:AROUND) (PERSON T) 4020263743> ; the primary method prints the name CL-USER > (defmethod print-object ((p person) stream) (format stream "~a" (slot-value p 'name))) #<STANDARD-METHOD PRINT-OBJECT NIL (PERSON T) 40202646BB> ; a new class id-mixin provides an id CL-USER > (defclass id-mixin () ((id :initarg :id))) #<STANDARD-CLASS ID-MIXIN 422034A7AB> ; the print method just prints the value of the id slot CL-USER > (defmethod print-object :after ((object id-mixin) stream) (format stream " ID: ~a" (slot-value object 'id))) #<STANDARD-METHOD PRINT-OBJECT (:AFTER) (ID-MIXIN T) 4020278E33> ; now we redefine the class person to include the mixin id-mixin CL-USER 241 > (defclass person (id-mixin) ((name :initarg :name))) #<STANDARD-CLASS PERSON 4220333E23> ; the existing instance *person-1* now has a new slot and we set it to 42 CL-USER 242 > (setf (slot-value *person-1* 'id) 42) 42 ; displaying the object again. The print-object function now has an effective method, which calls three methods: an around method, the primary method and the after method. CL-USER 243 > *person-1* #<PERSON Eva Luator ID: 42> </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)