[racket] Cascading dispatch rules
Hi André,
The example code you give is not correct with respect to the url
producing part of dispatch-rules. If I were to call (top-url
admin-posts) then it would fail because admin-posts is not on the RHS
of any dispatch case. There's no way to get that to work unless we
make a thicker protocol between dispatch-rules and the RHS so they
could look up further patterns. Doing this would be a fairly major
reworking, but probably a good idea.
We could make _ valid in dispatch cases. This may be a good place to
start to hack if want to work on the first thing. If you don't want to
do it, just make a issue on Github and I'll take care of it.
The dispatch container system is designed to address this problem in
dispatch-rules, but it requires an imperative interface. As mentioned
above, a functional one could exist but doesn't.
Jay
On Tue, Jan 20, 2015 at 10:27 AM, André Matheus <amatheus at mac.com> wrote:
> Hi,
>
> I'm currently learning racket, and started developing a web application.
> When using dispatch-rules, my first thought was to create separate dispatch
> rules and then call them from a top dispatch. Something like this:
>
> (dispatch-rules
> (("admin") admin-dispatch)
> (("api") api-dispatch)
> ... other rules)
>
> (define-values (admin-dispatch admin-url)
> ("" admin-index)
> ...)
>
> (define-values (api-dispatch api-url)
> ("" api-index)
> ...)
>
> If I understand correctly there's no simple direct way to do this. I managed
> to do it the
> following way:
>
> - defining the top level dispatch with string-arg's to catch all requests,
> and wrapping the dispatch function to use only the first argument (the
> request)
> (define-values (top-dispatch top-url)
> (dispatch-rules
> (("admin") (string-arg) ...) (lambda args (admin-dispatch (first
> args))))))
>
> - defining the other dispatch functions with the sub-path repeated
> (define-values (admin-dispatch admin-url)
> (dispatch-rules
> (("admin" "posts") admin-posts)))
>
> My questions:
>
> - is there an existing way to create this kind of cascading or nested
> dispatch rules?
> - is there a parameter matcher to match anything that I could use instead of
> (("path") (string-arg) ...) (lambda args (path-dispatch (first args))))
> or some better way to match everything starting with "path"?
>
> If there is no way I'm thinking of creating a macro to simplify, so that I
> don't need
> wrapping the dispatch functions, specifying parameters and then discarding
> and so on.
>
> Thanks,
>
> André
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>
--
Jay McCarthy
http://jeapostrophe.github.io
"Wherefore, be not weary in well-doing,
for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
- D&C 64:33