Not sure if this is the best place to post this, but I wondered if anybody could tell me if I'm doing this correctly. The idea is that I want named attributes for my object, and I only want to have to specify them once.
If this isn't too crazy, is there any way to make it the default behaviour for new objects? (EDIT: To clarify, I want to create a macro or something that will let me make it the default behaviour for new objects in my program, not the NewLisp distribution itself...)
Thanks!
Code: Select all
; h1. Node Class Test
; h2. Node Class
(new Class 'Node)
(set 'Node:parms '((title "Untitled")
(where (0 0))))
(define (Node:Node)
(let ((parms (copy Node:parms))
(parm nil)
(data (list Node)))
(doargs (i)
(setq parm (pop parms))
(if parm (push (list (parm 0) i) data -1)))
(append data parms)
))
(define (Node:get key)
(lookup key (self)))
(define (Node:set key value)
(setf (assoc key (self)) (list key value)))
; h2. Tests
(define (test what pass)
(println what "..." ('("FAILED" "OK") (if pass 1 0))))
(setq n (Node "Fantastiche"))
(test "Title set" (= "Fantastiche" (:get n 'title)))
(test "Default location" (= '(0 0) (:get n 'where)))
(:set n 'where '(10.0 14.5))
(test "New location" (= '(10.0 14.5) (:get n 'where)))