[racket] passing a list of dispatch clauses to dispatch-rules

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Mon Dec 16 17:44:10 EST 2013

On Mon, Dec 16, 2013 at 3:25 PM, Janos Tobias Locsei
<jtlocsei at cantab.net> wrote:
> Hi, I'm a newbie Racketeer so apologies if this is a silly question:
>
> Is it possible to pass a list of dispatch-clause to dispatch-rules? Naive
> use of "apply" doesn't work:
>
> (define-values (blog-dispatch blog-url)
>   (let ([arglist
>          (list
>             [("") list-posts]
>             [("posts" (string-arg)) review-post]
>             [("archive" (integer-arg) (integer-arg)) review-archive]
>             [else list-posts])])
>     (apply dispatch-rules arglist)))

dispatch-rules is a macro, so it cannot be "apply"d

> (example modified from docs.racket-lang.org/web-server/dispatch.html) This
> code gives rise to the error
>
> string-arg: this match expander must be used inside match in: (string-arg)

This error happens before you try to apply, beucase (string-arg) can
only be used inside of a dispatch case expression.

> Maybe there's something clever I could do with delay and force, but I'm not
> very familiar with how they work...
>
> Background: the reason I want to do this is so that I can dynamically
> construct a list of all the filenames in a directory of static html files
> and make dispatch clauses so that they can be accessed at URL's without
> ".html" on the end.

If you want it truly dynamic on every request, then I would just catch
[(string-arg) ...] and in the handler throw next-dispatcher if the
string doesn't correspond to a file.

Jay

>
> Thanks in advance for any guidance!
>
> Tobias
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93

Posted on the users mailing list.