Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Box could have better methods for writing and describing #306

Open
jpellegrini opened this issue Dec 1, 2021 · 4 comments
Open

Box could have better methods for writing and describing #306

jpellegrini opened this issue Dec 1, 2021 · 4 comments

Comments

@jpellegrini
Copy link
Contributor

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>

@egallesio
Copy link
Owner

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 pushed a commit that referenced this issue Dec 3, 2021
As required by the issue #306 signaled by @jpellegrini
@jpellegrini
Copy link
Contributor Author

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?

@egallesio
Copy link
Owner

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.

@jpellegrini
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants