Re: MAKE-INSTANCE and STRUCTURE-CLASS

"Tim Bradshaw (as tfb at cley dot com)" <[email protected]> Mon, 6 Jul 2026 09:02:17 +0100
Newsgroups gmane.lisp.lispworks.general,gmane.lisp.cl-pro
Message-ID <[email protected]>
--Apple-Mail-4B3AA4FE-839B-4007-B596-C9B9A47FB48C
Content-Type: text/plain;
	charset=utf-8
Content-Transfer-Encoding: quoted-printable

=EF=BB=BF
On 6 Jul 2026, at 07:05, Marco Antoniotti <[email protected]> wrote=
:
> But the question is whether there are major pitfalls doing something like t=
his:
>=20
> (defstruct foozzot a s d)
>=20
> (defmethod make-instance ((fz (eql 'foozzot)) &key &allow-other-keys)
>    (make-foozzot)) ; Munge initargs as you wish.


I suppose you could do that.  I mean, it's horrific, and it would violate th=
e expectation that make-instance when given a symbol calls make-instance on t=
he corresponding class.  I certainly have code which assumes it can use find=
-class to check a class exists and only then calls make-instance, so it's in=
 control of errors and this would break that.

> Of course it zound be nicer to handle STRUCTURE-CLASS and STRUCTURE-OBJECT=
; again, what are the contraindications?  After all, it seems to me that thi=
s is simply something the standard crafters punted on.

As I said: you can't do that in conforming code: you can't define methods on=
 standardised GFs which are applicable when all their arguments are direct i=
nstances of standardised classes.  There might be an exception for make-inst=
ance but I don't think so.

I realise it's violating the spirit of CLOS ('make it complicated, then add b=
ureaucracy' as Chapman would never have said), but my general-purpose constr=
uctor is funcall or apply, and curiously the same pair of functions are my g=
eneral-purpose equality predicates.  I just pass in constructor functions &c=
.

If I wanted to add bureacracy I'd define

(defgeneric constructor (class)
  (:method ((class symbol))
    (constructor (find-class class)))
  (:method ((class standard-class))
    (lambda (&rest kws &key &allow-other-keys)
      (apply #'make-instance class kws))))

... methods for constructor now can be defined for any class at all.






--Apple-Mail-4B3AA4FE-839B-4007-B596-C9B9A47FB48C
Content-Type: text/html;
	charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html class=3D"apple-mail-supports-explicit-dark-mode"><head><meta http-equi=
v=3D"content-type" content=3D"text/html; charset=3Dutf-8"></head><body dir=3D=
"auto"><div dir=3D"ltr">=EF=BB=BF<meta http-equiv=3D"content-type" content=3D=
"text/html; charset=3Dutf-8"><div dir=3D"ltr"><div dir=3D"ltr"></div><div di=
r=3D"ltr">On 6 Jul 2026, at 07:05, Marco Antoniotti &lt;marco.antoniotti@uni=
mib.it&gt; wrote:</div><div dir=3D"ltr"><blockquote type=3D"cite"><br></bloc=
kquote></div><blockquote type=3D"cite"><div dir=3D"ltr"><div>But the questio=
n is whether there are major pitfalls doing something like this:<br><br></di=
v><div><span style=3D"font-family:monospace">(defstruct foozzot a s d)<br><b=
r></span></div><div><span style=3D"font-family:monospace">(defmethod make-in=
stance ((fz (eql 'foozzot)) &amp;key &amp;allow-other-keys)<br></span></div>=
<div><span style=3D"font-family:monospace">&nbsp; &nbsp;(make-foozzot)) <spa=
n style=3D"color:rgb(255,0,0)">; Munge initargs as you wish.</span></span><b=
r></div></div></blockquote><div><br></div><div><br></div>I suppose you could=
 do that. &nbsp;I mean, it's horrific, and it would violate the expectation t=
hat make-instance when given a symbol calls make-instance on the correspondi=
ng class. &nbsp;I certainly have code which assumes it can use find-class to=
 check a class exists and only then calls make-instance, so it's in control o=
f errors and this would break that.</div><div dir=3D"ltr"><br><div><div><blo=
ckquote type=3D"cite"><div dir=3D"ltr"><div>Of course it zound be nicer to h=
andle STRUCTURE-CLASS and STRUCTURE-OBJECT; again, what are the contraindica=
tions?&nbsp; After all, it seems to me that this is simply something the sta=
ndard crafters punted on.</div></div></blockquote><br></div></div><div>As I s=
aid: you can't do that in conforming code: you can't define methods on stand=
ardised GFs which are applicable when all their arguments are direct instanc=
es of standardised classes. &nbsp;There might be an exception for make-insta=
nce but I don't think so.</div><div><br></div><div>I realise it's violating t=
he spirit of CLOS ('make it complicated, then add bureaucracy' as Chapman wo=
uld never have said), but my general-purpose constructor is funcall or apply=
, and curiously the same pair of functions are my general-purpose equality p=
redicates. &nbsp;I just pass in constructor functions &amp;c.</div><div><br>=
</div><div>If I wanted to add bureacracy I'd define</div><div><br></div><div=
>(defgeneric constructor (class)</div><div>&nbsp; (:method ((class symbol))<=
/div><div>&nbsp; &nbsp; (constructor (find-class class)))</div><div>&nbsp; (=
:method ((class standard-class))</div><div>&nbsp; &nbsp; (lambda (&amp;rest k=
ws &amp;key &amp;allow-other-keys)</div><div>&nbsp; &nbsp; &nbsp; (apply #'m=
ake-instance class kws))))</div><div><br></div><div>... methods for construc=
tor now can be defined for any class at all.</div><div><br></div><div><br></=
div><div><br></div><div><br></div><div><br></div></div></div></body></html>=

--Apple-Mail-4B3AA4FE-839B-4007-B596-C9B9A47FB48C--

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
[email protected]
http://www.lispworks.com/support/lisp-hug.html