Capturing a change in slot value.

Discussion of Common Lisp
Post Reply
macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Capturing a change in slot value.

Post by macrolyte » Sat May 31, 2014 6:13 pm

Hey.
I've been pouring over the Hyperspec and some other resources but haven't found a reasonable solution. Initially I played hide & seek with slot-boundp:

Code: Select all

defmethod initialize-instance :after ((node b-node) &key) ;; DON'T EVEN THINK ABOUT TRYING THIS!!!!!
 (loop while (slot-boundp node 'stat) do ;; test to see if a change in slot value would trigger an unbound condition, (non working code...)
      (print "started")) 
 (funcall (lambda()(print "done"))))
Please don't use the code above. I've looked at make-load-form-saving-slots as well but I would have to poll a parsed version of the value it returns which is just, ugh. My last option is to create an additional slot which I can use as a wayback machine... Thanks in advance.

pjstirling
Posts: 166
Joined: Sun Nov 28, 2010 4:21 pm

Re: Capturing a change in slot value.

Post by pjstirling » Sun Jun 01, 2014 5:28 am

I'm not entirely sure what you want to do here, do you want to:
  • check if the instance is created using the default value for a slot
  • fire some code when the slot is given a value the first/every time

macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Capturing a change in slot value.

Post by macrolyte » Sun Jun 01, 2014 10:07 am

pjstirling wrote:I'm not entirely sure what you want to do here, do you want to:
  • check if the instance is created using the default value for a slot
  • fire some code when the slot is given a value the first/every time
Sorry about being unclear, the "fire some code ..." is what I'm trying to do. I just realized that a function object can be a slot value so I've been trying to piece this together from that direction. Thanks!

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Capturing a change in slot value.

Post by Goheeca » Sun Jun 01, 2014 10:32 am

I don't know at which level do you want the capturing. That's my solution:

Code: Select all

(defclass foo ()
  ((bar :accessor bar-acc)))

(defmethod (setf bar-acc) :before (new-value (instance foo))
  (format t "~&Hey, you are setfing bar in this baz (~a) to ~a.~%" instance new-value))
and usage

Code: Select all

(defvar *obj* (make-instance 'foo))

(setf (bar-acc *obj*) t)
Just for higher meta level use MOP.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Capturing a change in slot value.

Post by macrolyte » Sun Jun 01, 2014 11:22 am

I need to read the Hyperspec, give me a few minutes. (It works BTW...). Thanks!
Last edited by macrolyte on Sun Jun 01, 2014 12:39 pm, edited 1 time in total.

macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Capturing a change in slot value.

Post by macrolyte » Sun Jun 01, 2014 11:52 am

Goheeca wrote:

Code: Select all


(defmethod (setf bar-acc) :before (new-value (instance foo)) ;; <===== see below
  (format t "~&Hey, you are setfing bar in this baz (~a) to ~a.~%" instance new-value))

Is this about the specialize lambda list with parameters matching types/objects thing? And where the @$%^ is the method name?
Goheeca wrote:
Just for higher meta level use MOP.
Huh?

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Capturing a change in slot value.

Post by Goheeca » Sun Jun 01, 2014 12:50 pm

Yes it is. The method name is bar-acc, the notation is for getter:

Code: Select all

(defmethod accessor ...)
and for setter:

Code: Select all

(defmethod (setf accessor) ...)
MOP is API for the object metaprogramming, the wrapping library negotiating implementation-dependent details is closer-mop.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Capturing a change in slot value.

Post by macrolyte » Sun Jun 01, 2014 3:09 pm

Thanks again, Goheeca. At least you've given me a starting point, so I'll get back to work now. Pax vobiscum.

Post Reply