Does glob ever return strings? If not, should we update the docs?

I've only been able to observe glob from file/glob returning paths, but it is documented as returning a (listof path-string?). Are there actually cases where it returns strings? A few quick experiments indicate it only returns path values (which is fine by me, since I'd like to sort the results and use them as paths, so having a uniform result type makes that easier). I couldn't follow the underlying code with just a few minutes of study, so I figured I'd ask here instead.

Although I didn't study it exhaustively, and could easily be wrong, I think it uses values coming from in-directory, whose contract says path-string?. So that's probably why file/glob should also use path-string?.

However:

Glancing at in-directory source, it uses a locally defined function dir-list:

  (define (dir-list full-d d acc)
    (for/fold ([acc acc]) ([f (in-list (reverse (sort (directory-list full-d) path<?)))])
	      (cons (build-path d f) acc)))

Those values come from a directory-list function defined #%kernel; I can't quickly view its definition. But notice how dir-list is sort-ing the values with path<?. That would fail unless all path? items -- can't be path-string?.

So: It seems to me that if the contract and doc for file/glob ought to change, then so should those for in-directory. That latter's doc already talks about sorting using path<?, so it kind of implies the contract could be path? instead of path-string?.

Also: It's not entirely clear to me, but I think the relevant contract might be path-for-some-system? -- not path? ???


p.s. I've always found path-string? confusing. It actually behaves like "path-or-path-string?":

  (define-values (path-string?)
    (lambda (s)
      (or (path? s) 
          (and (string? s)
               (or (relative-path? s)
                   (absolute-path? s))))))

Clean sheet of paper, it would probably be clearer to have all of:

  • path? i.e. a path struct, not a string
  • path-string? new meaning: string that is an absolute or relative path
  • path-or-path-string? i.e. what today's path-string? means
  • The little conversions that everyone seems to write: path-or-path-string->path and path-or-path-string->path-string?.

But naming is hard and backward compatible naming is harder.

1 Like

@greghendershott In glob: clarify that only path values are returned by benknoble · Pull Request #5047 · racket/racket · GitHub I've added you as a coauthor for your help using the author information that seems most recently associated with you, but GitHub doesn't connect it to your GitHub account. Let me know if I should try something different.

  1. Thank you but no need to co-author me on the commit.

  2. Request: Could you please delete the portion of the post above which shows my email addresses?

(I realize anyone can get these using git tools/repos. But I don't like leaving these around on web sites like this, where web scrapers can slurp for spam. Web sites like GitHub and pkgs.racket-lang.com obscure email addresses, so same spirit please?)

(And no worries. I'm not upset. Just my preference. Thanks!!)

Done! (Turns out it was a typo, anyway, which I’ll also fix elsewhere when I get back to my computer.)

1 Like