Hi!
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!
; 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)))
The way I do this is to define class constants:
> (context (new Class 'Point))
Point
Point> (constant 'x 1 'y 2)
2
Point> (define (move new-x new-y) (setf (self x) new-x (self y) new-y) (self))
(lambda (new-x new-y) (setf (self x) new-x (self y) new-y) (self))
Point> (context MAIN)
MAIN
> _
Within the context we can use (self x) and (self y) to refer to the attributes. Outside the class we use the fully qualified name:
> (setq p1 (Point 0 0))
(Point 0 0)
> (:move p1 10 20)
(Point 10 20)
> (p1 Point:x)
10
> (p1 Point:y)
20
> _
m i c h a e l