I've been trying to write a simple web service in Prolog and was wondering how I can handle optional parameters. I thought that with library(http/http_parameters)
it may be as simple as that:
my_request_handler(Request) :-
http_parameters(Request, [ param_1(Param1, []), param_2(Param2, []) ]),
... ### handle both parameters
my_request_handler(Request) :-
http_parameters(Request, [ param_1(Param1, []) ]),
... ### handle only param_1
so if param_2
is not provided the engine will backtrack to the second rule. But in SWI-Prolog http_parameters
raises exception if parameters do not match the specification and so the code breaks on the first rule rather than trying to evaluate the second one.
Unfortunately, adding optional(true)
to the param_2
specification makes it unbound and forces me to use a conditional check like this:
my_request_handler(Request) :-
http_parameters(Request, [ param_1(Param1, []), param_2(Param2, [optional(true)]) ]),
(error:text(Param2) ->
... ### handle both parameters
;
... ### handle only param_1
).
Is this the best way of doing things or I am missing something? I guess, that the conditionals become much uglier if more than one parameter is optional...
Cheers,