This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

setf.scm



A setf! implementation in the form of setf.scm has been added to guile.

This differs most markedly from code I posted a while back (with a
similar interface but rather different implementation) in that setter
is a macro, and setf! attempts to determine the setter of the passed
procedure at macro-expansion time, not run-time. I think this is a bad
idea because it _appears_ to be dynamic, by being based on the
procedure object rather than the symbol, unlike Common Lisp, but it
actually isn't because of the macro-expansion property described
above.

For instance, code like this:

(define (mutate-cons-cell accessor cell new-value)
  (setf! (accessor cell) new-value))

(define my-cell (cons 3 4))
(define my-cell (cons 3 4))

(mutate-cons-cell car my-cell 'x)

(mutate-cons-cell cdr my-cell 'y)

Will silently do the shockingly unexpected wrong thing and leave
my-cell with a value of (y . 4), whereas a fully Common Lisp-like setf
would have complained, and my code would have done the right thing. I
think the current code is thus the worst of all possible worlds,
semantics-wise. (I predicted this on reading the code, but testing
confirms).

Performance-wise it may be a bit better than what I posted but I don't
think that's a good enough justification for the misfeature described
above. 

I also don't think it is useful to allow getters and setters to be
macros (and obviously that can't be done with the non-memoizing
version where setter is a procedure since code written to use that
with macros would be utterly uncompilable). Is there a particular
purpose that was envisioned for?

 - Maciej