STklos icon indicating copy to clipboard operation
STklos copied to clipboard

Box could have better methods for writing and describing

Open jpellegrini opened this issue 2 years ago • 4 comments

Writing a box does not show its contents

stklos> (define a (constant-box 1 2))
;; a
stklos> a
#[box (2) 7f2d0200cd80]

And describing it doesn't mention it is a box. It also doesn't say if it is mutable or not.

stklos> (describe a)
#[box (2) 7f2d0200cd80] is an an instance of class <ref>.

For example, this is what Gauche does:

gosh$ (define a (box 1 2))
a
gosh$ a
#<mv-box[2] 1 2>
gosh$ (describe a)
#<mv-box[2] 1 2> is an instance of class <mv-box>

jpellegrini avatar Dec 01 '21 07:12 jpellegrini

Hi @jpellegrini,

You are right using the name <ref> is not a good idea. The changelog tells that the change was done long time ago because the name was used by old GTk boxes. There is no more reason to have this name, and I revert <ref> to <box>. About writing boxes showing their values is not the convention used elsewhere and this is "dangerous" if we have a circularity (I STklos has already this problem, with mono-value boxes which have a specific notation) For instance,

stklos> (define b (box 1 2))
;; b
stklos> b
#[box (2) 7fef4e1352a0]
stklos> (set-box-value! b 0 b)
stklos> b
#[box (2) 7fef4e1352a0]
stklos> 

This code segfaults on gosh (and on STklos if b uses only one value too).

About describe, we can do better, and the next commit will be more detailled:

stklos> (define b (box 1 2))
;; b
stklos> (describe b)
#[box (2) 7fcb503042c0] is an instance of class <box>.
It is mutable and contains 2 values.
Its content is:
    1
    2
stklos> (set-box-value! b 0 b)
stklos> (describe b)
#[box (2) 7fcb503042c0] is an instance of class <box>.
It is mutable and contains 2 values.
Its content is:
    #[box (2) 7fcb503042c0]
    2

BTW, I'm just asking myself if writing mono-boxes with the #& notation is really a good idea (we can keep it for reading)

egallesio avatar Dec 03 '21 16:12 egallesio

Hi @egallesio ! Thanks for looking into this. I was thinking that boxes could be handled in a similar way to lists, and their write procedure would then take care of checking circularity... If this is too complex, then maybe it's better to not show the contents, as you have suggested?

jpellegrini avatar Dec 03 '21 17:12 jpellegrini

Yes because circularity can be indirect (a circular list which contains a circular box). The code for circularity knows only about conses and vectors. I'm not sure that the effort is worth it.

stklos> (define l '#0=(1 . #0#))
;; l
stklos> (define b (box l))
;; b
stklos> b
#&#0=(1 . #0#)

but as soon as you do

stklos> (set-car! l b)

you have a list which contains a box with a circularity.

egallesio avatar Dec 09 '21 16:12 egallesio

Thanks for the explanation @egallesio !

I was thinking more of something like

  • describe on a box/list/vector/etc is the usual full, recursive describe
  • while describing each member of the box/list/vector/etc, only include something like ,#<list> ,#<box> etc.

But even that would be kind of lengthy work I think.

jpellegrini avatar Dec 09 '21 18:12 jpellegrini