This is the mail archive of the guile@sourceware.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]

Re: Syntatic sugar and identifier permissivity


Lalo Martins <lalo@hackandroll.org> writes:

> Like it or not, syntatic sugar is a fundamental part of what
> makes a language good for day-to-day use. As I like to say, 95%
> of Python (the language) is syntatic sugar.
> 
> === GOOPS ===
> 
> And slot-ref/slot-set is really bad on this aspect.

And they will be removed.

> Even accessors are bad because when you're coding you don't want to
> think you're running a function with your object as an argument
> (which is what an accessor looks like, and it's more or less what it
> actually is, but that is off the point).

I think this is a question of getting used to it.  It seems to me to
be a rather optimal way of using Scheme syntax to express accessing an
object.  It's also nice that there are no other operations on objects
than calls to generic functions.

(BTW, when someone has improved the method compiler, accessing slots
through accessors will, in most cases, be translated to a static
reference to an offset within the object.  I say this to point out
that regarding slot access as a call to a generic function imposes no
restrictions on the implementation.)

> (print foo,bar)
>
> is equivalent to
> 
> (print (slot-ref foo 'bar))

(We're not just going to remove the procedure `slot-ref'.  We're going
 to remove *names* from GOOPS.  Names are only going to be handled by
 the name space handler (aka module system).  So the above syntax
 would have to be translated into

 (print (bar foo))

)

This may not be at all how you arrived at this syntax, but maybe
you're influenced by class-centered object systems.  In an
operation-centered object system, like GOOPS, I think (bar foo) is a
more natural way of accessing a component of an object.

Also, there should be very strong reasons before we introduce new
syntax into the language.  Firstly, I don't think foo,bar is better
enough than (bar foo) to motivate new syntax.  Secondly, we will soon
have `with-slots' and `with-accessors' so that you can write:

(with-slots (bar) foo
  ...
  (set! bar (* bar bar))
  ...)

(I include an incomplete implementation below.)

> However, this is currently not possible because Guile accepts
> _anything_ (as far as I've tested) in symbols, as long as the
> first character is valid. I don't know about r4rs, but r5rs
> tells me this is wrong.
> 
> So IMHO Guile should be a little more strict about this

Yes.


Here's the (incorrect) with-slots implementation.  It works as long as
you don't use slot names in lexical bindings.

(define with-slots
  (procedure->memoizing-macro
    (lambda (x e)
      (let ((names (cadr x))
	    (obj (caddr x))
	    (body (cdddr x)))
	`(begin
	   ,@(wslots-transform-application body e names obj))))))

(define (wslots-transform x e names obj)
  (cond ((symbol? x)
	 (if (memq x names)
	     `(slot-ref ,obj ',x)
	     x))
	((not (pair? x)) x)
	((eq? (car x) 'quote) x)
	((eq? (car x) 'set!)
	 (if (memq (cadr x) names)
	     `(slot-set! ,obj
			 ',(cadr x)
			 ,(wslots-transform (caddr x) e names obj))
	     (cons 'set! (wslots-transform-application (cdr x) e names obj))))
	((list? x)
	 (wslots-transform-application x e names obj))
	(else x)))

(define (wslots-transform-application x e names obj)
  (map (lambda (x) (wslots-transform x e names obj))
       x))

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