Code: Select all
(define-macro (Foo:set)
(letex ((inst (args 0))) (set-assoc (inst 'key) (args 1))))
Code: Select all
(define-macro (Foo:set)
(letex ((inst (args 0))) (set-assoc (inst 'key) (args 1))))
Code: Select all
(define (Class:Class) (cons (context) (args)))
(new Class 'Book)
(define (Book:check-out b) (set-assoc (b 'status) '(status "Out")) (set '$obj b))
(define (Book:check-in b) (set-assoc (b 'status) '(status "In")) (set '$obj b))
(:check-out (Book "Catcher in the Rye" "J.D. Salinger" '(status "In")))
$obj => (Book "Catcher in the Rye" "J.D. Salinger" (status "Out"))
(:check-in $obj)
$obj => (Book "Catcher in the Rye" "J.D. Salinger" (status "In"))
I'm not completely sure I understand your question, but I'll just say something and hope it ends up being an answer :-)Jeff wrote:Tell me, is the context the same for all instances? Or does it clone the context as in 'new with each instance?
Code: Select all
(define (Class:Class) (cons (context) (args)))
(new Class 'Book)
(context 'Library)
(set 'book (Book "Catch 22" "Joesph Heller"))
(context MAIN)
Library:book => (Book "Catch 22" "Joesph Heller")
Code: Select all
(new Class 'Foo)
Code: Select all
(define (Foo:bar inst) (context (first inst) "bar" "baz"))
Jeff wrote:What I wanted to know was if the context that is the car of the instance list is a copy of the Foo instance, with its own internal tree, or the Foo instance itself.
Jeff wrote:One would mean that all variables stored in the context itself are static/class variables. Changes to one are reflected in all instances immediately.
Code: Select all
(define (Foo:bar? f) (= (f 1) "bar"))
(:bar? '(Foo "bar" "baz")) => true ; notice the use of the quote. necessary without a constructor
(:bar? '(Foo "baz")) => nil
Code: Select all
(context 'Point)
(setq x 0 y 0) ; default slot values
(define (print-point)
(println (format "(%d, %d)" x y)))
(context MAIN)
(new Point 'my-point)
(setq my-point:x 4 my-point:y 12)
(my-point:print-point) ; => prints "(4, 12)"
Code: Select all
(context 'Counter)
(define (Counter:Counter ctx (start-from 0))
(new (context) ctx)
(setq ctx (eval ctx))
(setq ctx:current start-from))
(define (increment (n 1)) (inc 'current n))
(define (decrement (n 1)) (dec 'current n))
(context MAIN)
(Counter 'counter 100)
(counter:increment 10)
(counter:increment 12)
(println counter:current) ; => 122
Yes 'new' makes a copy of a context, a new namespace with a new set of new symbols in it, which have the same name and a copy of the contents of the original. If symbols already exist, their context will not be overwritten.I was just wondering if the context call in the Class functor was in fact cloning the context.
No anonymous context creation is possible. In older versions you could create a context on a local symbol (local, let, letn, lambda), but I am in the process of eliminating this possibility. Currently it will hang in many situations or give an error message. It should not be done.had once seen a post by Lutz hinting at (context) creating anonymous contexts in local scopes that got me wondering
No, an 'object' defined in such a way is not a prototype-based OO at all. That my-point 'object' has not a refererence to its class neither its prototype, not only it is not a prototyped object but it is not even an object, by definition an object knows the class it belongs to and this object doesn't know so it's not an object, in prototype-based OO there's no class, everyting is an object and you create a new object from another object as prototype, so the new created object has a reference to its prototype, the object used as model for creation, this my-point 'object' has no reference to its prototype so it is not a protoype-based object. The only reason to call it an object is by the fact it's a data structure that holds data and behaviour.Jeff wrote: ↑Wed Jun 04, 2008 12:39 pm
Previously, if I wanted to write in an object-oriented style:
Now, if something changed in the context Point, it would not change in my-point. This is prototype-based OO, just as in Javascript.Code: Select all
(context 'Point) (setq x 0 y 0) ; default slot values (define (print-point) (println (format "(%d, %d)" x y))) (context MAIN) (new Point 'my-point) (setq my-point:x 4 my-point:y 12) (my-point:print-point) ; => prints "(4, 12)"
with this code you are using a context to represent an object, that's ok, the problem is you are pretending the object is also an instance of a class, and that's not the case, you are using context to represent both classes and objects, an pretending Counter is a (class) constructor for instance objects of that class, which is not. WithJeff wrote: ↑Wed Jun 04, 2008 12:39 pmTherefore, if I wanted to implement a counter using a closure, I could do so like this:
Code: Select all
(context 'Counter) (define (Counter:Counter ctx (start-from 0)) (new (context) ctx) (setq ctx (eval ctx)) (setq ctx:current start-from)) (define (increment (n 1)) (inc 'current n)) (define (decrement (n 1)) (dec 'current n)) (context MAIN) (Counter 'counter 100) (counter:increment 10) (counter:increment 12) (println counter:current) ; => 122
Code: Select all
(Counter 'counter 100)
Code: Select all
(define (classname) (context))
yes I think you are ;-)
Code: Select all
(context 'Counter)
(define (start (n 0)) (set 'current n))
(define (increment (n 1)) (inc current n))
(define (decrement (n 1)) (dec current n))
(context MAIN)
(Counter:start 100)
(Counter:increment 10)
(Counter:increment 12)
(println Counter:current) ; => 122