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!
===Object runtime alteration=== This example shows how an existing instance can be changed to include a new slot when its class changes and that an existing method can be replaced with a new version. <syntaxhighlight lang="lisp"> ; a person class. The person has a name. CL-USER > (defclass person () ((name :initarg :name))) #<STANDARD-CLASS PERSON 4020081FB3> ; a custom printing method for the objects of class person 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) 4020066E5B> ; one example person instance CL-USER > (setf *person-1* (make-instance 'person :name "Eva Luator")) #<PERSON Eva Luator> ; the class person gets a second slot. It then has the slots name and age. CL-USER > (defclass person () ((name :initarg :name) (age :initarg :age :initform :unknown))) #<STANDARD-CLASS PERSON 4220333E23> ; updating the method to print the object CL-USER > (defmethod print-object ((p person) stream) (print-unreadable-object (p stream :type t) (format stream "~a age: ~" (slot-value p 'name) (slot-value p 'age)))) #<STANDARD-METHOD PRINT-OBJECT NIL (PERSON T) 402022ADE3> ; the existing object has now changed, it has an additional slot and a new print method CL-USER > *person-1* #<PERSON Eva Luator age: UNKNOWN> ; we can set the new age slot of instance CL-USER > (setf (slot-value *person-1* 'age) 25) 25 ; the object has been updated CL-USER > *person-1* #<PERSON Eva Luator age: 25> </syntaxhighlight> let foo = 42; // foo is now a number foo = "bar"; // foo is now a string foo = true; // foo is now a boolean
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)