[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

ISO advise about print-object

Jim Newton

7/2/2015 8:15:00 AM

I'm never sure quite how defensive I need to be when writing a print-object method.
Here is an example, and I'm happy to hear anyone's advise.

(defclass A ()
((slot1 :accessor get-slot1 :initarg slot1)))

(defmethod print-object ((self A) stream)
(if (slot-boundp self 'slot1)
(print-unreadable-object (self stream :type t :identity nil)
(format stream "slot1=~A" (slot-value self 'slot1)))
(call-next-method)))

My question confusion deals with whether to get the value of slot1 by calling slot-value or by calling the accessor. In my mind one purpose of the accessor is to abstract the idea that the information is stored in a slot. For example a subclass of A might define a method named get-slot1 which is not a simple accessor but which perhaps calls call-next-method with some additional logic.

If print-object calls the accessor, get-slot1, does it really need to check whether the slot is bound before calling it?

The reason I'd like to check whether the slot is bound is so I can get nicer results in the stack trace and debugger if there is an error in the instance initialisation when the instance might not yet be fully initialized.

Does anyone have a strong opinion or some useful advise?

14 Answers

Siebe de Vos

7/2/2015 9:27:00 AM

0

> I'm never sure quite how defensive I need to be when writing a
> print-object method.
> Here is an example, and I'm happy to hear anyone's advise.
>
> (defclass A ()
> ((slot1 :accessor get-slot1 :initarg slot1)))
>
> (defmethod print-object ((self A) stream)
> (if (slot-boundp self 'slot1)
> (print-unreadable-object (self stream :type t :identity nil)
> (format stream "slot1=~A" (slot-value self 'slot1)))
> (call-next-method)))
>
> My question confusion deals with whether to get the value of slot1 by
> calling slot-value or by calling the accessor. In my mind one purpose of
> the accessor is to abstract the idea that the information is stored in a
> slot. For example a subclass of A might define a method named get-slot1
> which is not a simple accessor but which perhaps calls call-next-method
> with some additional logic.
>
> If print-object calls the accessor, get-slot1, does it really need to
> check whether the slot is bound before calling it?
>
> The reason I'd like to check whether the slot is bound is so I can get
> nicer results in the stack trace and debugger if there is an error in
> the instance initialisation when the instance might not yet be fully
> initialized.
>
> Does anyone have a strong opinion or some useful advise?


My lazy solution is:

(format stream "slot1=~A"
(or (ignore-errors (get-slot1 self)) "??"))

Then when you're not lazy you will add multiple-value-bind (when nil is
a possible value), somehow print the error, put all in a function
(slot-value-text object slot) based on MOP introspection, but:

- being too verbose can be a nuisance too
- be careful with cyclical data -- a typical problem with the standard
print-object of structures
- in critical situations the smart printer might make things worse;
think of out of memory, corrupted memory, etc.
- you're lazy anyway

So maybe you should leave all that to an inspector.

S.

Barry Margolin

7/2/2015 3:02:00 PM

0

In article <87egkrj64c.fsf@kuiper.lan.informatimago.com>,
"Pascal J. Bourguignon" <pjb@informatimago.com> wrote:

> There's no print-object in ISO Lisp.

ISO is also an abbreviation for "In Search Of".

There's no such thing as ISO Lisp, it's called ISLisp.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Kaz Kylheku

7/2/2015 5:56:00 PM

0

On 2015-07-02, Barry Margolin <barmar@alum.mit.edu> wrote:
> In article <87egkrj64c.fsf@kuiper.lan.informatimago.com>,
> "Pascal J. Bourguignon" <pjb@informatimago.com> wrote:
>
>> There's no print-object in ISO Lisp.
>
> ISO is also an abbreviation for "In Search Of".
>
> There's no such thing as ISO Lisp, it's called ISLisp.

But that's an abbreviation for ISO Standard Lisp, where "Standard"
is totally redundant. (Could there exist an "ISO Lisp" which is distinct from
"ISO Standard Lisp"?

I have a problem with the name, in any of its forms, because it's basically
claiming the unqualified "Lisp" name for itself. Adding ISO isn't a
qualification!

"ISO Lisp" means that some people cooked up a language, called it "Lisp"
without qualification contrary to McCarthy's express wish, and then pushed that
language through ISO.

It is an obvious "ethical corollary" of McCarthy's wish that no one Lisp
dialect should standardize and call itself "ISO Lisp", in contrast to
other dialects being "ISO This Lisp" or "ISO That Lisp".

The "ISLisp" abbreviation is a mockery also; an obvious play on "is Lisp".
As in, "this is Lisp". "ISLisp is Lisp!". Note lack of indefinite article "a".

Barry Margolin

7/2/2015 9:18:00 PM

0

In article <20150702104559.470@kylheku.com>,
Kaz Kylheku <kaz@kylheku.com> wrote:

> On 2015-07-02, Barry Margolin <barmar@alum.mit.edu> wrote:
> > In article <87egkrj64c.fsf@kuiper.lan.informatimago.com>,
> > "Pascal J. Bourguignon" <pjb@informatimago.com> wrote:
> >
> >> There's no print-object in ISO Lisp.
> >
> > ISO is also an abbreviation for "In Search Of".
> >
> > There's no such thing as ISO Lisp, it's called ISLisp.
>
> But that's an abbreviation for ISO Standard Lisp, where "Standard"
> is totally redundant. (Could there exist an "ISO Lisp" which is distinct
> from
> "ISO Standard Lisp"?

I assumed it was for International Standard Lisp.

But if it's redundant, so what? Have you never said "PIN number" or "ATM
machine"?

>
> I have a problem with the name, in any of its forms, because it's basically
> claiming the unqualified "Lisp" name for itself. Adding ISO isn't a
> qualification!

Yeah, we (X3J13) weren't happy with it when it happened.

But ISLisp has had very little impact, so we don't really care much
about it.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Pascal J. Bourguignon

7/3/2015 8:10:00 AM

0

Barry Margolin <barmar@alum.mit.edu> writes:

> In article <20150702104559.470@kylheku.com>,
> Kaz Kylheku <kaz@kylheku.com> wrote:
>
>> On 2015-07-02, Barry Margolin <barmar@alum.mit.edu> wrote:
>> > In article <87egkrj64c.fsf@kuiper.lan.informatimago.com>,
>> > "Pascal J. Bourguignon" <pjb@informatimago.com> wrote:
>> >
>> >> There's no print-object in ISO Lisp.
>> >
>> > ISO is also an abbreviation for "In Search Of".
>> >
>> > There's no such thing as ISO Lisp, it's called ISLisp.
>>
>> But that's an abbreviation for ISO Standard Lisp, where "Standard"
>> is totally redundant. (Could there exist an "ISO Lisp" which is distinct
>> from
>> "ISO Standard Lisp"?
>
> I assumed it was for International Standard Lisp.
>
> But if it's redundant, so what? Have you never said "PIN number" or "ATM
> machine"?
>
>>
>> I have a problem with the name, in any of its forms, because it's basically
>> claiming the unqualified "Lisp" name for itself. Adding ISO isn't a
>> qualification!
>
> Yeah, we (X3J13) weren't happy with it when it happened.
>
> But ISLisp has had very little impact, so we don't really care much
> about it.

On the other hands, some people who'd like a revised cleaned-up standard
might like it over CLâ?¦

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Jim Newton

7/3/2015 10:00:00 AM

0

aaaaaahhhhhhhh!!!! Now I understand why Pascal was talking about which Lisp I'm using and about IS lisp. I was wondering why I was being chastised for asking a question about Common Lisp on comp.lang.lisp
My topic was "ISO advise about print-object". By that I meant "In Search Of advise about print-object".
Sorry, but for me ISO means "in search of".
Sorry about the confusion.

Pascal J. Bourguignon

7/3/2015 10:31:00 AM

0

Jim Newton <jimka.issy@gmail.com> writes:

> aaaaaahhhhhhhh!!!! Now I understand why Pascal was talking about which
> Lisp I'm using and about IS lisp. I was wondering why I was being
> chastised for asking a question about Common Lisp on comp.lang.lisp
> My topic was "ISO advise about print-object". By that I meant "In Search Of advise about print-object".
> Sorry, but for me ISO means "in search of".
> Sorry about the confusion.

Yeah, I've only been reading English on the Internet 8 hours a day for
30 years, it's the first time I see ISO meaning "In Search Of".

The closest I've come to this expression was "Desperately Seeking
Susan". ;-)

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Jim Newton

7/3/2015 11:07:00 AM

0

Casper H.S. Dik

7/3/2015 11:24:00 AM

0

"Pascal J. Bourguignon" <pjb@informatimago.com> writes:

>The closest I've come to this expression was "Desperately Seeking
>Susan". ;-)

A long time ago, the first LaserWriter we had was called
"Mr. Goodbar" because the message from the systems was
"Looking for <printer>"; but because Appletalk was rather
unreliable, the next printer was called "Susan" and we
changed the the computers to print

"Desperately Seeking <printer>".

Casper

smh

7/4/2015 12:14:00 AM

0

Now that all the trolls have beaten the use of ISO into bitpulp, perhaps we can return to the original question. A couple mostly-separate comments:

It is obviously helpful if a print-object method can be implemented defensively so that it never (aka "rarely") itself signals an error. One common blowup is when initialization of a complex standard-object errors, and the print-object method for that object invoked in the debugger retrieves some slot that has not yet been initialized. Makes bedugging [sic] harder than it need be [sic].

Keep in mind that print-object methods might be written and intended for help during development and debugging, or might be written and used as a tool as part of a runtime application. Unfortunately, print-object is too restricted to be the machinery to support both. The gf specializes only on the object and the output stream. Customizing stream classes so print-object can determine for which context it is being invoked is like putting the cart before the egg, or the chicken before the horse. pprint-dispatch tables are a better tool for the purpose of runtime printing in an application. Details left as an exercise for the student.

Finally, for both purposes I've long thought that simple print-object is insufficient. Suppose a print-object method on a class with many mixins wants to use print-unreadable-object. If the body of its print-unreadable-object calls call-next-method on the same object (not on one of the object's "slots") then it is likely the printed output will contain multiple nested #<....> notations. This isn't good. It would have been better if some 2-level protocol could have been made a convention -- that a class could define a print-object-internals possible in addition to its print-object method. The first print-object method for an object could avoid calling call-next-method and instead call print-object-internals so it and its subclasses could print human-readable versions of the desired object properties. Haven't thought through the detailed API.