[racket-dev] racket/format

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Sep 7 10:27:59 EDT 2012

I've added a `racket/format' library that is a revised version of
Ryan's `unstable/cat'. It's re-exported by `racket', but not by
`racket/base'.

I think Ryan's library is a step in the right direction for string
formatting, and the difficult remaining task is getting the names
right. So, this `racket/format' experiment changes all of the names.

As reflected in the `racket/format' library name, the new function
names are intended to leverage your experience with `format': the `cat'
function is `~a', the `catp' function is `~v', and the `catw' function
is `~s'. The `catn' functions are collapsed to a single `~r'.

Using this library, instead of writing

  (format "The result of\n  ~s\nis\n  ~v"
	  expr 
          (eval expr))

you'd write

  (~a "The result of\n"
      "  " (~s expr) "\n"
      "is\n"
      "  " (~v (eval expr)))

or even, when using `#lang at-exp racket',

  @~a{The result of
        @~s[expr]
      is
        @~v[(eval expr)]}

Keyword options like `#:width' and `#:align', of course, can be an even
bigger help for formatting.

I concede that names like `~s' are not remotely mnemonic unless you've
already learned the somewhat obscure `format' escapes, but it seems
difficult to find compact names that are meaningful and that don't
cause collisions; we can at least take advantage of the compact names
that we've already learned. Embracing tildes also means that we worry
less about replacing all sorts of APIs, such as `error', that have
`format' built in. And maybe `~a' works relatively well as a shorthand
for `string-append'.


Posted on the dev mailing list.