[plt-scheme] Re: Typed Scheme and match with struct patterns

From: Paulo J. Matos (pocmatos at gmail.com)
Date: Mon Mar 23 22:17:27 EDT 2009

On Tue, Mar 24, 2009 at 2:09 AM, Paulo J. Matos <pocmatos at gmail.com> wrote:
> Hi all,
>
> Even though I already submitted this as a bug (10153) I wonder if
> someone has a workaround in the meantime:
> Check the following program:
> #lang typed-scheme
>
> (require scheme/match)
>
> (define-type-alias mytypes (U foobar foo bar))
>
> (define-struct: foobar
>  ((useless : Integer)))
>
> (define-struct: foo
>  ((name : Symbol)))
>
> (define-struct: (bar foo)
>  ((val : String)))
>
> (: match-bug (mytypes -> Void))
> (define (match-bug f)
>  (match f
>    ((struct foobar (i))
>     (display i))
>    ((struct foo _)
>     (printf "Name : ~a\n" (foo-name f))
>     (when (bar? f)
>       (printf "Val : ~a\n" (bar-val f))))))
>
> Running it returns:
> typecheck: Wrong function argument type, expected foo, got mytypes for
> argument 1 in: f
>
[snip]

Just noticed I can use
((struct bar (name val)) ...)
as pattern. Thought I couldn't include inherited fields in the
pattern. This solves the issue but only as a workaround because forces
you to make sure the patterns are in the right order. You can't have a
pattern for foo before the pattern for bar otherwise it won't work!

Cheers,

-- 
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm


Posted on the users mailing list.