I need a function which returns a list of all derived subclasses for a given
class. It should contain the class itself. I wrote the following:
(defun class-and-subclasses (class)
(let ((s (class-direct-subclasses class)))
(if s
(cons class (mapcan 'class-and-subclasses s))
(list class))))
I'm not sure if this is a good solution or if one can think of a better way.
Any suggestions?
Thanks
Andreas
"Andreas Thiele" <······@nospam.com> writes:
> I need a function which returns a list of all derived subclasses for a given
> class. It should contain the class itself. I wrote the following:
>
> (defun class-and-subclasses (class)
> (let ((s (class-direct-subclasses class)))
> (if s
> (cons class (mapcan 'class-and-subclasses s))
> (list class))))
>
> I'm not sure if this is a good solution or if one can think of a better way.
>
> Any suggestions?
looks fine to me.
--
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There is a crack in everything.
That's how the light gets in.
-Leonard Cohen
"Marco Baringer" <··@bese.it> writes:
> "Andreas Thiele" <······@nospam.com> writes:
>
>> I need a function which returns a list of all derived subclasses for a given
>> class. It should contain the class itself. I wrote the following:
>>
>> (defun class-and-subclasses (class)
>> (let ((s (class-direct-subclasses class)))
>> (if s
>> (cons class (mapcan 'class-and-subclasses s))
>> (list class))))
>>
>> I'm not sure if this is a good solution or if one can think of a better way.
>>
>> Any suggestions?
>
> looks fine to me.
To me, it looks wrong:
[96]> (class-and-subclasses (find-class 'a))
*** - Lisp stack overflow. RESET
(defclass a () ())
(defclass b (a d) ())
(defclass c (b) ())
(defclass d (c) ())
Try rather:
[97]> (defun class-and-subclasses (class)
(com.informatimago.common-lisp.utility:compute-closure
(function class-direct-subclasses) (list class)))
CLASS-AND-SUBCLASSES
[98]> (class-and-subclasses (find-class 'a))
(#<STANDARD-CLASS A> #<STANDARD-CLASS B :INCOMPLETE>
#<STANDARD-CLASS C :INCOMPLETE> #<STANDARD-CLASS D :INCOMPLETE>)
[99]>
--
__Pascal Bourguignon__ http://www.informatimago.com/
Pascal Bourguignon <···@informatimago.com> writes:
> To me, it looks wrong:
>
> [96]> (class-and-subclasses (find-class 'a))
>
> *** - Lisp stack overflow. RESET
>
>
> (defclass a () ())
> (defclass b (a d) ())
> (defclass c (b) ())
> (defclass d (c) ())
this is a circular class hierarchy, isn't that reason enough to not
expect his code to deal with it? (what lisp lets you define those
clasess?)
--
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There is a crack in everything.
That's how the light gets in.
-Leonard Cohen
"Pascal Bourguignon" <···@informatimago.com> schrieb im Newsbeitrag
···················@thalassa.informatimago.com...
> "Marco Baringer" <··@bese.it> writes:
>
> > "Andreas Thiele" <······@nospam.com> writes:
> >
> >> I need a function which returns a list of all derived subclasses for a
given
> >> class. It should contain the class itself. I wrote the following:
> >>
> >> (defun class-and-subclasses (class)
> >> (let ((s (class-direct-subclasses class)))
> >> (if s
> >> (cons class (mapcan 'class-and-subclasses s))
> >> (list class))))
> >>
> >> I'm not sure if this is a good solution or if one can think of a better
way.
> >>
> >> Any suggestions?
> >
> > looks fine to me.
>
> To me, it looks wrong:
>
> [96]> (class-and-subclasses (find-class 'a))
>
> *** - Lisp stack overflow. RESET
>
>
> (defclass a () ())
> (defclass b (a d) ())
> (defclass c (b) ())
> (defclass d (c) ())
>
> Try rather:
>
> [97]> (defun class-and-subclasses (class)
> (com.informatimago.common-lisp.utility:compute-closure
> (function class-direct-subclasses) (list class)))
>
> CLASS-AND-SUBCLASSES
> [98]> (class-and-subclasses (find-class 'a))
> (#<STANDARD-CLASS A> #<STANDARD-CLASS B :INCOMPLETE>
> #<STANDARD-CLASS C :INCOMPLETE> #<STANDARD-CLASS D :INCOMPLETE>)
> [99]>
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
OK. Thanks for the hint. Fixing this like you explained is easy. But, I use
this function in conjunction with a metaclass which unfortunately does not
allow such circular inheritence. Now the question which makes me anxious is,
if there is any sensible application of such a circular inheritance?
Andreas
Pascal Bourguignon <···@informatimago.com> writes:
> (defclass a () ())
> (defclass b (a d) ())
> (defclass c (b) ())
> (defclass d (c) ())
What would be the class precedence list of D?