[racket] Pattern matching on structures defined using make-struct-type

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Sun Feb 20 17:36:45 EST 2011

On Sun, Feb 20, 2011 at 2:14 PM, Marco Maggesi <marco.maggesi at gmail.com> wrote:
> Hi,
> is it possible to use patterns like (struct struct-id (pats)) to match
> structures which are defined with make-struct-type?
> Example:
> (define-values (s:tuple make-tuple tuple? tuple-ref tuple-set!)
>   (make-struct-type 'tuple #f 1 0 #f))
> (match (make-tuple (vector 'a))
>   [(struct tuple (a)) a])
> gives the error "match: tuple does not refer to a structure definition in:
> tuple"

The `struct' form in `match' uses the static struct info created by
`struct' and `define-struct'.  If you have a dynamically-specified
struct as you do here, you could manually create that struct info (see
the docs [1]) and bind that to `tuple' statically:

  (define-syntax tuple ...)

[1] http://docs.racket-lang.org/reference/structinfo.html
sam th
samth at ccs.neu.edu

Posted on the users mailing list.