[racket] Reader constructor for #<path:....>

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Jul 26 08:43:59 EDT 2010

At Mon, 26 Jul 2010 14:29:57 +0200, Laurent wrote:
> The reader cannot read #<path:....> forms (is this the right term?).
> When the interaction window is in constructor mode, for (build-path "a"
> "b"), it writes #<path:a/b>, but this cannot be read back.
> First question: would it be possible to make the reader read paths?

The reason that paths are not `read'able is that's there is not a
single right choice for how to marshal paths:

 * Sometimes you want the string form of a path, which may be encoded
   in different ways on different platforms (e.g., using different
   locales) to keep the string form the same.

 * Sometimes you want the bytes form of a path, because the path isn't
   going to be used on multiple systems, and converting to a string
   form may lose information (e.g., because it's not a UTF-8 encoding).

Since there was no right answer, we decided not to pick either of them.
The lack of a `read'able form is a weak hint to programmers that they
need to look closely at the question.

> Second question: I need this for my own purposes, so I wrote a
> path-constructor:
> (define/provide (write-path p)
>   (cons 'build-path
>         (map (λ(p-elt)(if (symbol? p-elt)
>                           (list 'quote p-elt)
>                           (path->string p-elt)))
>              (explode-path p))))
> > (write-path (build-path 'same 'up "a" "b"))
> '(build-path 'same 'up "a" "b")
> Does someone know if this is good enough, or am I omitting something
> (platform specific maybe)?

As a minimum, use `path-element->string' instead of `path->string'.

Otherwise, beware that (as noted above) not all paths have string
encodings; depending on your application, that may not be an issue. Or
it may be that you want to marshal via byte strings using `bytes->path'
as a constructor.

Posted on the users mailing list.