3 ms·
> But let's face it; CLOS has problems. One obvious one is that length isn't a polymorphic function. It's one of the first speed bumps you encounter. Ah that .
by kazinator 6y ago
> But let's face it; CLOS has problems. One obvious one is that length isn't a polymorphic function. It's one of the first speed bumps you encounter.
Ah that ... here is my worse-is-(not-necessarily)-better object system with single dispatch, and broken type redefining:
This is the TXR Lisp interactive listener of TXR 240.
Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet.
1> (defstruct foo ()
(:method length (me) 42))
#<struct-type foo>
2> (new foo)
#S(foo)
3> (len *2)
42
4> (defstruct bar ()
a d
(:method car (me) me.a)
(:method cdr (me) me.d))
#<struct-type bar>
5> (new bar a 1 d 2)
#S(bar a 1 d 2)
6> [mapcar list #S(bar a 1 d #S(bar a 2 d #S(bar a 3)))]
((1) (2) (3))
7> (sum #S(bar a 1 d #S(bar a 2 d #S(bar a 3))))
6
8> (length #S(bar a 1 d #S(bar a 2 d #S(bar a 3))))
3
9> (defstruct xyzzy ()
(h (hash))
(:method lambda (me key) [me.h key])
(:method lambda-set (me key val) (set [me.h key] val)))
#<struct-type xyzzy>
10> (new xyzzy)
#S(xyzzy h #H(()))
11> [*10 'x]
nil
12> (set [*10 'x] 1)
1
13> (set [*10 'y] 2)
2
14> (set [*10 'z] 3)
3
15> *10
#S(xyzzy h #H(() (z 3) (x 1) (y 2)))
16> (len *10)
** length: #S(xyzzy h #H(() (z 3) (x 1) (y 2))) has no length or car method
** during evaluation at expr-16:1 of form (len *10)
** run with --backtrace to enable backtraces
17> (defmeth xyzzy length (me) (len me.h))
(meth xyzzy length)
18> (len *10)
3