I’m trying to develop a URL dispatcher to execute a different action depending on the URL. For example:
- http://localhost:8080/ -> go to homepage
- http://localhost:8080/edit?id=2 -> go to edit page
- http://localhost:8080/delete?id=4 -> go to delete page
As for now I got the following code that works:
dispatch(uri) =
match uri with
| {path= [edit] query=[("id", parameter)] ...} -> Customer.edit(parameter)
| {path= [delete] query=[("sid", parameter)] ...} -> Customer.delete(parameter)
| {~path ...} -> start()
server = Server.simple_dispatch(dispatch)
However in my first attempt I had:
| {path= [edit] query=["id", parameter] ...} -> Customer.edit(parameter)
| {path= [delete] query=[("id", parameter)] ...} -> Customer.delete(parameter)
But the compiler throws an error because of having “id” in both lines, apparently the first line is “overriding” the second one. My questions are:
- How to keep the same parameter name “id” for both lines without having an error?
- How more than one parameter with different type can be handle? for example localhost:8080/edit?id=2&ask=true&showImage=0
- What is the pattern “…” for?
Thanks in advance
I don’t think the code you showed really works :). Try for instance:
and you will see that wrong resources are dispatched. The problem is that you put
editanddeletewithout quotes, so they are treated as pattern variables and will match anything (and be bound to the matched value). And that’s exactly why your first variant didn’t work — those two patterns are the same up-to variable renaming (edit/delete). What you want (I think) is:How to have more than one parameter?
Just extend your pattern matching (first line) to:
But this kind of matching is dangerous as it will only work if there are exactly those query parameters and in exactly this order. If you cannot be sure about that better do:
now the order does not matter and if the parameter is missing the default value (after
?) will be used.What is the pattern “…” for?
You are matching against an
urivariable of type:Uri.relative, which has more fields than justpathandquery....means there may be more fields in the record but we don’t care about them (without dots the records would be expected to have only explicitly mentioned fields).