Page 1 of 1

FOOP Scoop

Posted: Tue Feb 05, 2008 12:49 am
by m i c h a e l
FOOP Scoop!

If this is the mother of all constructors:

Code: Select all

(define (Class:Class) (cons (context) (args)))
Then this must be the daddy of all predicates:

Code: Select all

(define (Class:? obj) 
  (and (list? obj) (not (empty? obj)) (= (obj 0) (context)))
)
Of course, for this to have real meaning within the context of your code, you must use the fully qualified method name when applying it:

Code: Select all

> (Point:? (Point 23 54))
true
> (Complex:? (Complex 0.68 -1.64))
true
> (Complex:? (Point 0 0))
nil
> _
When using it this way, it's usually referred to as a class method.

The predicate's first two tests, (list? obj) and (not (empty? obj))), can become part of a function to test for objectness:

Code: Select all

(constant (global 'object?) 
  (fn (obj) 
     (and (list? obj) (not (empty? obj)) (context? (obj 0)))
  )
)
With that, you could define the predicate this way:

Code: Select all

(define (Class:? obj) (and (object? obj) (= (obj 0) (context))))
Even though these should be used as class methods, you can still apply the ? to an object using polymorphism:

Code: Select all

> (:? (Point 23 43))
true
> (:? (Complex 0.33 0.11))
true
> (:? (CornFlakes (CornFlake) (CornFlake) (CornFlake) ...))
true
> _
But in essence, all you're really asking the object is: are you your own type? :-)

m i c h a e l

P.S. Even the ellipsis in the CornFlakes object can become valid newLISP code:

Code: Select all

(set '... '...)
;-)

Posted: Tue Feb 05, 2008 10:06 pm
by itistoday
Thanks! What little treasures I find while leisurely browsing the newLISP forum instead of learning Cocoa...