- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello all,
I thought about implementing some sort of object oriented features within AutoLisp, similar to other OOP languages. As a proof of concept i wrote a few lines of code, but i'm stuck now.
My initial idea was to create a "custom object" withing AutoLisp and have the ability GET/SET its properties, in a similar way to VisualLisp objects.
I wrote a simple constructor for the object and a getter and setter, to read/write the object's properties. The getter works fine, but i can't get the setter function to work as i want it to.
For purposes of demonstration i've prepared a person object to illustrate my point:
(defun NewPerson (name gender heigt weight occupation) (list (cons "NAME" name) (cons "GENDER" gender) (cons "HEIGHT" heigt) (cons "WEIGHT" weight) (cons "OCCUPATION" occupation) ) ) ;defun (defun Get (obj prop) (cdr (assoc (vl-symbol-name prop) obj)) ) ;defun (defun Put (obj prop val) (if (assoc (vl-symbol-name prop) obj) (subst (cons (vl-symbol-name prop) val) (assoc (vl-symbol-name prop) obj) obj) (append obj (list (cons (vl-symbol-name prop) val))) ) ) ;defun
I can create a new person object using:
_$ (setq person1 (NewPerson "Bill" "Male" 191 98 "Car Dealer")) (("NAME" . "Bill") ("GENDER" . "Male") ("HEIGHT" . 191) ("WEIGHT" . 98) ("OCCUPATION" . "Car Dealer"))
I can get a property using:
_$ (get person1 'height) 191
And here comes my question. I want to be able to write (put person1 'height 185) and save the property within the person1 object.
The only way i can get it to work is to write (setq person1 (put person1 'height 185) ).
Is it possible to retrieve the variable name of an agrument that is supplied to a function. I want to be able to get the symbol name of the variable person1, without writing it as a quoted symbol: (put 'person1 'height 185).
In other words, is it possible to find out within my PUT function, that (obj = person1), without supplying person1 as a quoted symbol.
This is a proof of concept i want to show to a friend, who told me "Lisp is a functional programming language that is inferior to the modern object oriented programming languages."
I want to show him that Lisp is much more powerfull as a programming language, than many people think, but i want my demo to be perfect.
Im aiming for this syntax:
(get person1 'height)
(put person1 'height 185)
Solved! Go to Solution.