2013-07-30 27 views
0

问题,所以这Opa事情正在酝酿。
即时通讯开始这样的服务器:Opalang和与Server.start()参数

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}) 
     } 

然而即时得到:

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

所以它显然dispatch_handler_fun是正确的类型签名不是。 API文档,我可以看到Server.handler在一变 http://doc.opalang.org/type/stdlib.core.web.server/Server/handler

是明确向任何人为什么dispatch_handler_fun是不恰当的吗?
ps。比较遗憾的是糟糕的代码格式化:)

谢谢

回答

2

中有变量类型Server.handler,只有{string title, (→ xhtml) page}{ (Uri.relative → resource) dispatch }(无标题字段)没有{title, dispatch}

原因标题连接到page而不是dispatchpage仅返回xhtml没有HTML标头的正文。服务器需要一些额外的数据作为页面的标题。您使用的dispatch函数会返回附加了一些额外数据的资源,并且不需要将其提交给服务器两次。在您的示例中,您已经使用了函数Resource.page(),该函数将标题作为第一个参数。

+0

感谢您的详细解答。 :) - “你使用的分派函数返回附加了一些额外数据的资源,并且不需要两次给服务器。” - 你会推荐我在这里使用什么?因为'dispatch'预计是类型“(Uri.relative - > resource)”(即函数应该返回一个资源,就像我的函数一样)。 – deepblue

+0

所以我从Server.start()中删除了'title'成员,因为你建议由dispatch_handler_fun()创建的资源有自己的标题。现在应用程序编译时没有类型错误:) – deepblue

相关问题