From: Joe Marshall
Subject: Re: defgeneric, defstruct and no CLOS
Date: 
Message-ID: <d5ljt53p.fsf@alum.mit.edu>
verec <·····@mac.com> writes:

> Let's consider this current project I'm working on at the moment
> (http://lisp.jfb-city.co.uk)
>
> o I've got a file that contains 100,000 sorted english words.
>   I want to read that file from disk into a "someting". Let's
>   call that something: dict
>
> o I've got another file that contains a 15x15 grid that I also
>   want to read from disk into another something. Let's call
>   that: grid
>
> Obviously, I can define:
> (defun load-dict (dict-name) ...)
> and
> (defun load-grid (grid-name) ...)
>
> But that "noise in my head" I was referring to above is nagging
> me, and claims loudly to my hears: "This is crap! What you want
> is: (defun load (grid) ... ) and (defun load (dict) ...)"

Exactly where a generic function would be great.

> So, my question realy is, assuming both:
> (defstruct grid ...) and
> (defstruct dict ...)
>
> how would I go about
>
> (defun load (grid) ... ) and
>
> (defun load (dict) ... ) and while I'm there, not shadow the
>
> standard cl::load ?
>
> This suggests using packages as a means of encapsulation
> but that little voice is not too happy with that idea either.
>
> If this kind of "overloading" is not "the Lisp way" then that's
> fine with me too, but what would be "the Lisp way" in that case?

Separating the LOAD that you define from CL:LOAD is what you would do
with packages.  Frankly, that's more of a pain than I'd want to deal
with, so I'd define a new generic function like `LOAD-OBJECT'.

> The thing is, I've got quite a few "generic" verbs that I'd like
> to apply to any ``object''. In that example, and assuming that
> overloading can be made to work (and is "idiomatic"), I'd
> add to the party:
>
> (defun locate (grid x y)
> 	"Returns the word limit associated with coordinates
> 	x and y of the grid"
>       ... )
>
> and
>
> (defun locate (dict word)
> 	"Returns the index of the word if found, or the one's complement
>  	of the index this word would need to be inserted at if not
>  	found"
> 	...)
>
> In short: how can I make this overloading work, or, what would be a
> better (more idiomatic) alternative?

This kind of overloading you *wouldn't* use CLOS for.  A position in a
dictionary is a very different thing from a grid co-ordinate.