Print asymmetry with coops

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Print asymmetry with coops

EfraimVagner
Hi,

I've started to work on a small project for fun in scheme, and I wanted to use a class for one thing. I saw that there is the coops extension, that does what I want, but my problem is that when printing an object i get #<coops-instance>. I saw I can use describe-object to get a better description of the object. Another thing that I would like to have, is that in sbcl, for example (I think any common lisp implementation acts the same), when using defstruct, the printed version of the new object can be used to create another object. For example:

(defstruct point x y)

(make-point :x 1 :y 2) ;; Will print #S(POINT :X 1 :Y 2)

(setq p1 #S(POINT :X 10 :Y 0)) ;; p1 will be a point with x=10 and y=0

I don't like the way it works in scheme because it looses the symmetry of lisp. My question is then, how can I restore the symmetry? (btw, I'm using chicken scheme if it matters)

Thanks!

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users
Reply | Threaded
Open this post in threaded view
|

Re: Print asymmetry with coops

Peter Bex
On Fri, Apr 26, 2019 at 09:11:02AM +0000, EfraimVagner wrote:

> Hi,
>
> I've started to work on a small project for fun in scheme, and I wanted to use a class for one thing. I saw that there is the coops extension, that does what I want, but my problem is that when printing an object i get #<coops-instance>. I saw I can use describe-object to get a better description of the object. Another thing that I would like to have, is that in sbcl, for example (I think any common lisp implementation acts the same), when using defstruct, the printed version of the new object can be used to create another object. For example:
>
> (defstruct point x y)
>
> (make-point :x 1 :y 2) ;; Will print #S(POINT :X 1 :Y 2)
>
> (setq p1 #S(POINT :X 10 :Y 0)) ;; p1 will be a point with x=10 and y=0
>
> I don't like the way it works in scheme because it looses the symmetry of lisp. My question is then, how can I restore the symmetry? (btw, I'm using chicken scheme if it matters)
Hi Efraim,

You can define a custom record printer using define-record-printer:
https://wiki.call-cc.org/man/5/Module%20(chicken%20base)#define-record-printer

The example here also uses define-reader-ctor to read back the record.
If you prefer custom read syntax like in Common Lisp, you could also
use set-[sharp-]read-syntax! from (chicken read-syntax):
http://wiki.call-cc.org/man/5/Module%20(chicken%20read-syntax)#set-read-syntax

I think you should also be able to define custom read syntax for coops
instances as well.

Hope this helps,
Peter

_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Print asymmetry with coops

EfraimVagner


Thanks! This is what I was looking for.


‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Friday, April 26, 2019 12:17 PM, Peter Bex <[hidden email]> wrote:

> On Fri, Apr 26, 2019 at 09:11:02AM +0000, EfraimVagner wrote:
>
> > Hi,
> > I've started to work on a small project for fun in scheme, and I wanted to use a class for one thing. I saw that there is the coops extension, that does what I want, but my problem is that when printing an object i get #<coops-instance>. I saw I can use describe-object to get a better description of the object. Another thing that I would like to have, is that in sbcl, for example (I think any common lisp implementation acts the same), when using defstruct, the printed version of the new object can be used to create another object. For example:
> > (defstruct point x y)
> > (make-point :x 1 :y 2) ;; Will print #S(POINT :X 1 :Y 2)
> > (setq p1 #S(POINT :X 10 :Y 0)) ;; p1 will be a point with x=10 and y=0
> > I don't like the way it works in scheme because it looses the symmetry of lisp. My question is then, how can I restore the symmetry? (btw, I'm using chicken scheme if it matters)
>
> Hi Efraim,
>
> You can define a custom record printer using define-record-printer:
> https://wiki.call-cc.org/man/5/Module (chicken base)#define-record-printer
>
> The example here also uses define-reader-ctor to read back the record.
> If you prefer custom read syntax like in Common Lisp, you could also
> use set-[sharp-]read-syntax! from (chicken read-syntax):
> http://wiki.call-cc.org/man/5/Module (chicken read-syntax)#set-read-syntax
>
> I think you should also be able to define custom read syntax for coops
> instances as well.
>
> Hope this helps,
> Peter



_______________________________________________
Chicken-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/chicken-users