From: Norbert_Paul on
Hello,

I have defined a class where a slot *must* be initialized when creating
instances:

(define-condition modular-ring-without-generator (algebraic-error) () )

(defclass 'modular-ring (ring)
( (ring :initarg :ring :initform (make-instance 'ring) )
(generator :initarg :generator
:initform (error (make-condition
'modular-ring-without-generator ) ) ) ) )

which works, as HyperSpec says that initform is not evaluated when
the user specifies an initial form in make-instance:

So (make-instance 'modular-ring) signals the above error, whereas
(make-instance 'modular-ring :generator 7) creates Z/7Z.

Is there a standard way in CLOS or are there standard LISP-idioms
to get a similar behaviour? I couldn't find in HyperSpec how slot
initialization can be made mandatory.

Norbert
From: Tim Bradshaw on
On 2010-01-10 19:44:10 +0000, Norbert_Paul
<norbertpauls_spambin(a)yahoo.com> said:

> s there a standard way in CLOS or are there standard LISP-idioms
> to get a similar behaviour? I couldn't find in HyperSpec how slot
> initialization can be made mandatory.

What you have (make the default initform signal an error) is what I do.
Real Programmers would probably define a metaclass (no, that's wrong,
Lisp Hippies would define a metaclass, Real programmers would not
consider a language where whitespace is important).

From: Tamas K Papp on
On Sun, 10 Jan 2010 20:44:10 +0100, Norbert_Paul wrote:

> Hello,
>
> I have defined a class where a slot *must* be initialized when creating
> instances:
>
> (define-condition modular-ring-without-generator (algebraic-error) () )
>
> (defclass 'modular-ring (ring)
> ( (ring :initarg :ring :initform (make-instance 'ring) )
> (generator :initarg :generator
> :initform (error (make-condition
> 'modular-ring-without-generator ) ) ) )
> )
>
> which works, as HyperSpec says that initform is not evaluated when the
> user specifies an initial form in make-instance:
>
> So (make-instance 'modular-ring) signals the above error, whereas
> (make-instance 'modular-ring :generator 7) creates Z/7Z.
>
> Is there a standard way in CLOS or are there standard LISP-idioms to get
> a similar behaviour? I couldn't find in HyperSpec how slot
> initialization can be made mandatory.
>
> Norbert

Certainly. I would just create an :after method for
initialize-instance, and check everything I want there. For example,

(defclass modular-ring ()
((ring :initarg :ring :initform 'something)
(generator :initarg :generator)))

(defmethod initialize-instance :after ((object modular-ring) &key &allow-other-keys)
(unless (slot-boundp object 'generator)
(error 'modular-ring-without-generator)))

(make-instance 'modular-ring :generator 'something-else) ; OK
(make-instance 'modular-ring) ; error

BTW, your indentation/formatting is pretty non-standard (you should learn
to use your editor to indent Lisp code properly), and you don't need to quote
the name of the class (I wonder how you got your code to run, I tried
it in SBCL and it tells me that "'MODULAR-RING is not a legal class
name.").

HTH,

Tamas
From: Kenneth Tilton on
Tim Bradshaw wrote:
> On 2010-01-10 19:44:10 +0000, Norbert_Paul
> <norbertpauls_spambin(a)yahoo.com> said:
>
>> s there a standard way in CLOS or are there standard LISP-idioms
>> to get a similar behaviour? I couldn't find in HyperSpec how slot
>> initialization can be made mandatory.
>
> What you have (make the default initform signal an error) is what I do.
> Real Programmers would probably define a metaclass (no, that's wrong,
> Lisp Hippies would define a metaclass, Real programmers would not
> consider a language where whitespace is important).
>

Hockey pucks. Real Lispers do not give a r*t's *ss about guarding
against programmer error. You need to turn over the helicopter keys and
get some rehab, you been doing java too long.

kxo

--
http://www.stuckonalgebra.com
"The best Algebra tutorial program I have seen... in a class by itself."
Macworld
From: Norbert_Paul on
Hello Kenneth,

in this case I'd prefer being a non-real (algebraic?, ...) programmer
by simply pasting together what he considers best habits from all communities
he comes in touch with.

Note that I am, in fact, trying to return from years of Java-programming
back to LISP. Ho did you guess?

Note. (/ 1 0) => error: division-by-zero (in HyperSpec)
Has the spec been written by non-real programmers, too?

Another hockey puck: I am also planning to use somethig like CLUnit.
JUnit is fun! So will be CLUnit, I'm sure.

Norbert

Kenneth Tilton wrote:
....
> Hockey pucks. Real Lispers do not give a r*t's *ss about guarding
> against programmer error. You need to turn over the helicopter keys and
> get some rehab, you been doing java too long.
>
> kxo
>