[ 
http://dev.sourcefabric.org/browse/LS-617?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=22945#action_22945
 ] 

Romain Beauxis commented on LS-617:
-----------------------------------

I am trying to define a wrapper for HTTP callbacks that would make it optional 
to return some informations. Here's some code:

  # Master endpoints controler
  def handler(~method,~protocol,~data,~headers,uri) =
    def f(current_handler, element) =
      x             = fst(element)
      endpoint      = fst(x)
      target_method = snd(x)
      handler       = snd(element)

      if string.match(pattern=endpoint, uri) and method == target_method then
        handler
      else
        current_handler
      end
    end

    handler = list.fold(f, fourofour, !endpoints)


    # Response callback, use default values if
    # no override is passed by the handler.
    def response(~code=200, ~headers=headers, data) =
      http_response(
        protocol=protocol,
        code=code,
        headers=headers,
        data=data)
    end

    handler(data=data, headers=headers, uri=uri, response)
  end

  harbor.http.register(port=port, "^#{prefix}.*", handler)

In the response callback, I use the information already available (headers) or 
expected by default (code) so that the handler would not have to return them if 
it does not need to.

Such a handler could then be:
def foo(~data, ~headers, ~uri, response)
  response("foo!")
end

Or, for a 404 handler:
def not_found(~data, ~headers, ~uri, response)
  resonse(code=404, "Not found, dude!")
end

The problem is that response has type (?code:int, ...) ->... while in 
not_found, its inferred type is
(code:int, ...) -> ... so this would not work..

Actually, even with type annotation this would still be annoying has each 
handler definition would have to annotate the response's callback type..

If you have a better idea I'd be very interested :-)

> We need type annotations..
> --------------------------
>
>                 Key: LS-617
>                 URL: http://dev.sourcefabric.org/browse/LS-617
>             Project: Liquidsoap
>          Issue Type: New Feature
>    Affects Versions: 1.0
>            Reporter: Romain Beauxis
>            Priority: Important
>
> Here's why:
> # def apply(f, x) = f(x=x) end;;
> apply : (((x:'a)->'b),'a)->'b = <fun>
> # def optional(~x="f") = "f" end;;
> optional : (?x:string)->string = fun (~x="f") -> "f"
> # apply(optional, "foo");;
> At line 316, char -125:
>   this value has type
>     (?x:_)->_ (infered at line 316, char -139--110)
>   but it should be a subtype of (the type of the v
> In OCaml:
> # let apply f x = f ~x:x ;;                
> val apply : (x:'a -> 'b) -> 'a -> 'b = <fun>
> # let optional ?(x="foo") = x;;            
> Warning 16: this optional argument cannot be erased.
> val optional : ?x:string -> string = <fun>
> # apply optional "gni";;                   
> Error: This expression has type ?x:string -> string
>        but an expression was expected of type x:'a -> 'b
> BUT:
> # let apply (f : ?x:'a -> 'b) x = f ~x:x ;;
> val apply : (?x:'a -> 'b) -> 'a -> 'b = <fun>
> # apply optional "gni";;                   
> - : string = "gni"
> In case you're wondering, I have a much more useful example that cannot work 
> without that...

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://dev.sourcefabric.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Savonet-devl mailing list
Savonet-devl@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/savonet-devl

Répondre à