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]

generic method names for collections


On Sun, 8 Nov 1998, Maciej Stachowiak wrote:

> 
> jglascoe@jay.giss.nasa.gov writes:
> > 
> > Imagine that we're choosing names for generic methods on collection data
> > structures...  now, what's wrong with this picture:
> > 
> > (set! vector 0 "foo")
> > (set! vector 0)      ;; oops, forgot the third argument, I'm screwed now!
> > 
> 
> If I were changing the language radically enough that making (set!
> vector 0 "foo") were legal, I'd instead opt for making the syntax be
> 
> (set! (vector-ref vector 0) "foo")
> 
> That's the solution that Common Lisp takes essentially, and there is
> no ambiguity or vast potential for error here.

no offense, but that's just plain ugly.  Maybe it's best that lists and
vectors not take part in the MOP.  (OTOH, maybe they should ???  maybe we
could call their MOP-partaking siblings "arrays" and "pairs") 

But anyway, I see no reason why generic method names need be tied down to
the meaningless triplet ref/set!/del!  We should choose more
understandable names for the benefit of Scheme-newcomers. 

> Or, assuming you had a generic "ref" (maybe "index" would be a better
> name in a generic function world, assuming indexing by integer is
> distinct from looking up by an arbitrary key or something, or maybe
> "dereference" if you want to be anally correct) :
> 
> (set! (ref vector 0) "foo")

better, but still ugly.

> In other words, if you want to make everything generic, setting a
> location that you can get in some way should be made generic.

I like the "same thing but under a different name" approach:

(define foo (make-mop-vector 7))  ;; mop-vector, perl-list, foo-bar ???
(vector-set! foo 2 "two")         ;; ERROR
(insert! foo 2 "two") => "two"
(vector-ref foo 2)                ;; ERROR
(lookup foo 2) => "two"     

(vector? foo) => #f
(vector? (slot-value foo 'proxy)) => #t

etc.

> > Too bad they weren't thinking of generic functions when they dreamed up
> > the dual meanings of "set!".
> > 
> 
> set! doesn't have a dual meaning, `vector-set!' and `set!' are
> separate operators that are similarly named but do different
> things. If I wanted to design a generic function universe within
> Scheme I wouldn't do it the way you said, so this similarity would not
> be an issue. Assuming I wanted to leave the non-generic functions in,
> I'd be consistent in naming non-generics for other things that had
> them though.

okay, strings having "set!" as a substring have different meanings. 
"vector-set!" and "set!" are obviously different animals.  I was just
lumping the two together because (in my feeble-minded way) I was imagining
that

$generic_name = $name;
$generic_name =~ s/\S+-(\S+)/$1/; 

would be your basic approach to choosing a generic method name.

> 
>  - Maciej
> 
> 
> 

	Jay Glascoe
	jglascoe@jay.giss.nasa.gov