[racket] srcloc structures, lists, vectors

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Wed Sep 14 14:31:38 EDT 2011

I wanted to double check my understanding on why source locations have
several representations in Racket.

One of them is the srcloc structure:

But there's also the list and vector representations, all which are
handled by the syntax/srcloc library in

When I first saw the multitude of representations, I was surprised and
confused: I expected there to be one canonical representation.

I made sense of this to myself with the following reasoning: it's
easier to serialize a vector or list than it is a struct.  In many
cases, I want to embed source locations somewhere into the output of a
macro, and it's a lot easier to do something like this:

#lang racket
(define-syntax (where stx)
  (syntax-case stx ()
     (with-syntax ([loc (vector (syntax-source stx)
                                (syntax-line stx)
                                (syntax-column stx)
                                (syntax-position stx)
                                (syntax-span stx))])

than it is to do the equivalent with a srcloc structure:

#lang racket
(define-syntax (where-2 stx)
  (syntax-case stx ()
     (with-syntax ([sloc #`(srcloc '#,(syntax-source stx)
                                   '#,(syntax-line stx)
                                   '#,(syntax-column stx)
                                   '#,(syntax-position stx)
                                   '#,(syntax-span stx))])

There's several more requirements that the struct approach needs, from
the use of the module and macro system to be able to bind "srcloc"
hygienically, to remembering to quote all the internal arguments.  So
it seems the vector approach is more lightweight.

That doesn't quite explain to me why there are both list and vector
representations for srcloc, though... In any event, it's something
that's been bothering me, because it's not obvious why there are so
many representations.

Posted on the users mailing list.