From: Lars Rune Nøstdal
Subject: virtual, static, member (?)
Date: 
Message-ID: <162d283e-3fd8-4c77-a935-07a39428c5a2@y18g2000yqn.googlegroups.com>
(let ((id-generators (make-hash-table :test #'eq :weakness :key)))
  (defmethod generate-id-for (object)
    (multiple-value-bind (id-generator found-p)
        (gethash (class-of object) id-generators)
      (if found-p
          (funcall id-generator)
          (prog1 0
            (setf (gethash (class-of object) id-generator)
                  (let ((id 0))
                    (lambda () (incf id)))))))))

..i found this interesting.. what's it called? .. what shall i name
it? .. i'm thinking i should create a macro so one can apply this
effect in other contexts (not only for "id" stuff)

From: Lars Rune Nøstdal
Subject: Re: virtual, static, member (?)
Date: 
Message-ID: <7e9a1c9d-6d47-467b-9819-1fb93c8a388e@j39g2000yqn.googlegroups.com>
err, ups .. correct version:

(let ((id-generators (make-hash-table :test #'eq :weakness :key)))
  (defmethod generate-id-for (object)
    (multiple-value-bind (id-generator found-p)
        (gethash (class-of object) id-generators)
      (if found-p
          (funcall id-generator)
          (prog1 0
            (setf (gethash (class-of object) id-generators)
                  (let ((id 0))
                    (lambda () (incf id)))))))))
From: budden
Subject: Re: virtual, static, member (?)
Date: 
Message-ID: <4b74e6f1-cc0f-4caf-ba43-ebae8b3bf014@t2g2000yqm.googlegroups.com>
Looks like a memoization (there are libraries for that in www.cl-user.net)
From: Kenny
Subject: Re: virtual, static, member (?)
Date: 
Message-ID: <4925f7b8$0$4889$607ed4bc@cv.net>
Lars Rune N�stdal wrote:
> err, ups .. correct version:
> 
> (let ((id-generators (make-hash-table :test #'eq :weakness :key)))
>   (defmethod generate-id-for (object)
>     (multiple-value-bind (id-generator found-p)
>         (gethash (class-of object) id-generators)
>       (if found-p
>           (funcall id-generator)
>           (prog1 0
>             (setf (gethash (class-of object) id-generators)
>                   (let ((id 0))
>                     (lambda () (incf id)))))))))

I am alarmed at identity being identical only in conjunction with 
class-of. Wassup wid dat?

kzo
From: Rainer Joswig
Subject: Re: virtual, static, member (?)
Date: 
Message-ID: <joswig-554EF8.01225421112008@news-europe.giganews.com>
In article 
<····································@y18g2000yqn.googlegroups.com>,
 Lars Rune N�stdal <···········@gmail.com> wrote:

> (let ((id-generators (make-hash-table :test #'eq :weakness :key)))
>   (defmethod generate-id-for (object)
>     (multiple-value-bind (id-generator found-p)
>         (gethash (class-of object) id-generators)
>       (if found-p
>           (funcall id-generator)
>           (prog1 0
>             (setf (gethash (class-of object) id-generator)
>                   (let ((id 0))
>                     (lambda () (incf id)))))))))

What does it do? How is the number an ID?

Typically I would expect that an ID is associated with
some object and that there is a way to find out

* two objects have a different id, so they are different objects
* given an id I can find the corresponding object
* given an object, I can find the id

To me it counts for each class how many instances
of that class the function has seen.

I personally also don't like the style that much:

* why is it a method? 

* DEFMETHOD is a top-level macro, you are using it non top-level.

* Why use a closure? Why not hold the hash-table
  in a global variable. Then the hash-table
  and the function can be updated and inspected
  independent of each other.
  This is just hard to debug code.


> 
> ..i found this interesting.. what's it called? .. what shall i name
> it? .. i'm thinking i should create a macro so one can apply this
> effect in other contexts (not only for "id" stuff)

-- 
http://lispm.dyndns.org/
From: Lars Rune Nøstdal
Subject: Re: virtual, static, member (?)
Date: 
Message-ID: <06785226-85c5-4665-b8dd-0d6569a13ec6@k36g2000yqe.googlegroups.com>
On Nov 21, 1:22 am, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@y18g2000yqn.googlegroups.com>,
>  Lars Rune Nøstdal <···········@gmail.com> wrote:
>
> > (let ((id-generators (make-hash-table :test #'eq :weakness :key)))
> >   (defmethod generate-id-for (object)
> >     (multiple-value-bind (id-generator found-p)
> >         (gethash (class-of object) id-generators)
> >       (if found-p
> >           (funcall id-generator)
> >           (prog1 0
> >             (setf (gethash (class-of object) id-generator)
> >                   (let ((id 0))
> >                     (lambda () (incf id)))))))))
>
> What does it do? How is the number an ID?

it doesn't return the id; it generates an id .. look at the name


> * why is it a method?
>
> * DEFMETHOD is a top-level macro, you are using it non top-level.
>
> * Why use a closure? Why not hold the hash-table
>   in a global variable. Then the hash-table
>   and the function can be updated and inspected
>   independent of each other.

hm - yeah, that'd work
From: Kaz Kylheku
Subject: Re: virtual, static, member (?)
Date: 
Message-ID: <20081120174046.818@gmail.com>
On 2008-11-20, Lars Rune N�stdal <···········@gmail.com> wrote:
> (let ((id-generators (make-hash-table :test #'eq :weakness :key)))
>   (defmethod generate-id-for (object)
>     (multiple-value-bind (id-generator found-p)
>         (gethash (class-of object) id-generators)
>       (if found-p
>           (funcall id-generator)
>           (prog1 0
>             (setf (gethash (class-of object) id-generator)
>                   (let ((id 0))
>                     (lambda () (incf id)))))))))

This is somewhat related to association classes (in the UML model).

I.e. the associations between classes (the lines connecting their boxes) can
have properties as if they were classes.

For instance an association between a Student and a Course might have a grade
property. Neither the Student object nor the Course object has a grade, but the
association between them does.

You can hack this up in Lisp using hash tables.  The association between a
Student and Course can be defined (in an anonymous way, perhaps) and instances
of the assocation can be generated implicitly in some (get-association
<student> <class>) type generic function, which caches things in a hash table.

You'd use a weak hash table for this, just like the one above.

In your case, there is no association to anything, so the ID basically behaves
like a slot. You're associating a named value with an object. An object has an
ID. So there is no difference between this and making a slot called ID in the
object.

Suppose your object could exhibit a different ID with respect to different
objects. Then that would be an association property.

This also smacks of certain aspects of ... aspect oriented programming.  Namely
the ability to inject hidden slots into a class without modifying its
definition, so that aspects have a place to stash additional data needed
by the aspect advice methods.