Object Philosophy...

Pondering the philosophy behind the language

Object Philosophy...

Postby oofoe » Sun Jan 20, 2013 7:43 pm


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...)


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)))
Testing can show the presence of bugs, but not their absence.
Posts: 60
Joined: Wed Sep 28, 2005 7:13 pm

Re: Object Philosophy...

Postby m i c h a e l » Mon Jan 21, 2013 10:58 am

The way I do this is to define class constants:
Code: Select all
> (context (new Class 'Point))
Point> (constant 'x 1 'y 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)
> _

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:

Code: Select all
> (setq p1 (Point 0 0))
(Point 0 0)
> (:move p1 10 20)
(Point 10 20)
> (p1 Point:x)
> (p1 Point:y)
> _

m i c h a e l
m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA

Return to Whither newLISP?

Who is online

Users browsing this forum: No registered users and 1 guest