Page 1 of 1

[cells] don't evaluate drifter on make-instance

Posted: Thu Feb 25, 2010 12:11 pm
by hewih
i have an ephemeral cell which is NIL and a drifter cell which depends on the ephemeral cell. my problem is that the body of the drift cell
gets evaluated when the class is instantiated. thus i always have to check the ephemeral against NIL. is there a way to avoid evaluating the drift body on class instantiation? the drift cell has a initial value anyway...

Code: Select all

(cells-reset)
(defmodel drifter ()
  ((drift :cell :ephemeral :initform (c-formula (:inputp T :lazy :until-asked) (break "asked") NIL) :accessor drift-of)
   (val :initarg :val :accessor val-of))
   (:default-initargs ; if you remove this whole block then (break "asked") won't be called
        :val (c... (50)
  	           (if (null (^drift-of)) ; how do avoid this???
                  (progn (format T "~&initial drift?") 1)
                  (let ((d (- 50 (random (^drift-of)))))
					(format T "~&drift: ~S" d)
                     d)))))
(defparameter drifter1 (make-instance 'drifter))

(val-of drifter1)
(setf (drift-of drifter1) 100)
(val-of drifter1)
in my real application the ephemeral cell is from another class and is probably NIL when the drifter is instantiated so i cannot set it to something "useful" :/
i also tried to make the ephemeral cell lazy but it is asked as soon as the drifter cell is initialized.

Re: [cells] don't evaluate drifter on make-instance

Posted: Thu Feb 25, 2010 12:59 pm
by ramarren
The rule for the drifter cell has to be ran on instance creation so that its dependencies can be established. And it cannot be lazy, since lazy drifters would be rather missing the point. And in any case by the time the drifter would be asked for value the ephemeral would be reset anyway. And you have to handle the null value in ephemeral if the drifter depended on anything else that could change.

The only slight simplification would adding a method on c-value-incf specialized on your value and NULL, which would just return the value. Then you could wrap the rule with WHEN, rather than having to create a full if branch.

Or just create a macro like:

Code: Select all

(defmacro c...-maybe ((value zero &rest dependencies) &body body)
  `(c... (,value)
     (if (and ,@dependencies)
         (progn ,@body)
         ,zero)))

Re: [cells] don't evaluate drifter on make-instance

Posted: Thu Feb 25, 2010 1:45 pm
by hewih
> The rule for the drifter cell has to be ran on instance creation so that its dependencies can be established.

is there a way to define the dependencies without running the body so i can totally avoid the ephemeral NIL value check and not only hide it away? thanks for the macro snippet!

> And in any case by the time the drifter would be asked for value the ephemeral would be reset anyway.

i hope i misunderstood you and the drifter isn't recalculated and using a NIL ephemeral every time i access it's value... or the house of cards in my brain will completely break down :shock:

> And you have to handle the null value in ephemeral if the drifter depended on anything else that could change.

i "without-c-dependency" the non-ephemeral dependencies away. thus i only get the NIL value on instantiation time.

> The only slight simplification would adding a method on c-value-incf specialized on your value and NULL, which would just return the value. Then you could wrap the rule with WHEN, rather than having to create a full if branch.

that's an idea, thx! :)

one more question. can i define a initial value for ruled cells "c?" without doing an NIL value check on a ephemeral every time?

(sorry if i may sound confusing but i'm working since 11h and my brain is starting to melt...)

Re: [cells] don't evaluate drifter on make-instance

Posted: Thu Feb 25, 2010 2:18 pm
by ramarren
hewih wrote:is there a way to define the dependencies without running the body
I suppose there is a way in the sense that you could just go into Cells internals and create the links manually, but that doesn't really sound like a good idea.
hewih wrote:i hope i misunderstood you and the drifter isn't recalculated and using a NIL ephemeral every time i access it's value...
I meant that for the case of a hypothetical lazy drifter.
hewih wrote:one more question. can i define a initial value for ruled cells "c?" without doing an NIL value check on a ephemeral every time?
The idea of ephemerals is that the have the value of NIL at all times other that directly after their value was set. If you never plan on dependent values seeing that NIL then why is the cell ephemeral at all? If it makes sense for it to be ephemeral for some cases, but not others, you might want to create a non-ephemeral cell mirroring the last value of ephemeral one. Which would also solve the drifter case. For values of 'solve' including pushing the NIL check somewhere else.