0

so this Opa thing is picking up.
Im starting a Server like this:

    function resource request_dispatch(Uri.relative p_url,
                                       p_log_fun) {
            //type Uri.relative = {list(string) path, list((string,string)) query }
            match (p_url) {
                case {path: [] ... }              : get_standard_page(p_log_fun);
                case {path: ["_rest_" | path] ...}: api_request_handler(path,p_log_fun);
                case {~path ...}                  : Resource.page("Regular",<div>standard page</div>);
            }
        }     
function start_server(p_log_fun) {
            function resource dispatch_handler_fun(Uri.relative p_url) {
                request_dispatch(p_url,p_log_fun)
            }
            Server.start(Server.http,
                         { title   : "Hello, world",
                           dispatch:dispatch_handler_fun})
        }

however Im getting:

Error: File "src/posts_viewer.opa", line 71, characters 3-150, (71:3-74:42 | 2466-2613)
Type Conflict
(72:10-74:41) {dispatch: (Uri.relative -> resource); title: string } /
'c.a
(71:3-71:8) Server.handler
The second argument of function should be of type
{ dispatch: (Uri.relative -> resource); title: string } / 'c.a
instead of
Server.handler

so its clearly dispatch_handler_fun is not of the correct type signature. in the API docs I can see that Server.handler in a variant http://doc.opalang.org/type/stdlib.core.web.server/Server/handler

is it clear to anyone why dispatch_handler_fun is not appropriate here?
ps. sorry for the bad code formating :)

thank you

deepblue
  • 8,426
  • 13
  • 48
  • 60

1 Answers1

2

There is no {title, dispatch} in variant type Server.handler, only {string title, (→ xhtml) page} and { (Uri.relative → resource) dispatch } (no title field).

The reason title is connected to page and not to dispatch is the page returns only xhtml body without the HTML header. The server need some additional data as the title of the page. The dispatch function you use returns the resource which has some extra data attached and there is no need to give it to the server twice. You already used the function Resource.page() in your example, which takes the title as the first parameter.

Marcin Skórzewski
  • 2,854
  • 1
  • 17
  • 27
  • thanks for a detailed answer. :) -- "The dispatch function you use returns the resource which has some extra data attached and there is no need to give it to the server twice." -- what would you recommend I use here? since 'dispatch' is expected to be of type "(Uri.relative -> resource)" (aka the function should return a resource, as do my functions). – deepblue Jul 31 '13 at 16:43
  • so I removed the 'title' member from Server.start(), since you suggested that the resources created by dispatch_handler_fun() have their own title. now the app compiles with no type errors :) – deepblue Jul 31 '13 at 20:42