unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Implementing a "write" method for self-evaluating objects (by equal?)
@ 2020-09-17 20:02 divoplade
  2020-09-17 20:17 ` John Cowan
  0 siblings, 1 reply; 2+ messages in thread
From: divoplade @ 2020-09-17 20:02 UTC (permalink / raw)
  To: guile-user

Hello guile users,

In the manual, I read:

(define-method (write (file <named-file>) port)
  (format port "#<<file> ~s ~a>" (name file) (fd file)))

So it is possible to define a write method for foreign objects (and
more generally GOOPS classes).

My question is: why use such a bizarre syntax with # and <? Would it
not be better to make it self-evaluating:

  (format port "(make <named-file> #:fd ~a #:name ~s)"
    (fd file) (name file))

This is a little more verbose, but at least the syntax explains how to
construct the value. There must be some cases where it would not be
possible (such as pointers to opaque data that do not have a
constructor...), and it would break  if for instance "make" is renamed
to something else. However, if I were to implement a write method, I
would use this, a textual representation of a constructor that would
produce the same value (according to equal?).




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Implementing a "write" method for self-evaluating objects (by equal?)
  2020-09-17 20:02 Implementing a "write" method for self-evaluating objects (by equal?) divoplade
@ 2020-09-17 20:17 ` John Cowan
  0 siblings, 0 replies; 2+ messages in thread
From: John Cowan @ 2020-09-17 20:17 UTC (permalink / raw)
  To: divoplade; +Cc: guile-user

In general you want to print records but not make them readable, as there
are often constraints against allowing arbitrary values in the fields.  For
example, in SRFI 117 list queues, there are two fields named head and
tail.  Either both must be the empty list, or they must both be pairs and
tail must be a tail of head: that is, if you apply cdr to head repeatedly,
you will eventually get a value that is eq? to tail.  Allowing other random
values to be read in would break the SRFI 117 procedures.


On Thu, Sep 17, 2020 at 4:12 PM divoplade <d@divoplade.fr> wrote:

> Hello guile users,
>
> In the manual, I read:
>
> (define-method (write (file <named-file>) port)
>   (format port "#<<file> ~s ~a>" (name file) (fd file)))
>
> So it is possible to define a write method for foreign objects (and
> more generally GOOPS classes).
>
> My question is: why use such a bizarre syntax with # and <? Would it
> not be better to make it self-evaluating:
>
>   (format port "(make <named-file> #:fd ~a #:name ~s)"
>     (fd file) (name file))
>
> This is a little more verbose, but at least the syntax explains how to
> construct the value. There must be some cases where it would not be
> possible (such as pointers to opaque data that do not have a
> constructor...), and it would break  if for instance "make" is renamed
> to something else. However, if I were to implement a write method, I
> would use this, a textual representation of a constructor that would
> produce the same value (according to equal?).
>
>
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-09-17 20:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-17 20:02 Implementing a "write" method for self-evaluating objects (by equal?) divoplade
2020-09-17 20:17 ` John Cowan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).