Example Suave.IO does not compile in my F # project

I am trying to get this example from compiling Suave.io for a simple F # project: http://suave.io/

open Suave.Http.Applicatives open Suave.Http.Successful open Suave.Web open Suave.Types open Suave.Model let greetings q = defaultArg (q ^^ "name") "World" |> sprintf "Hello %s" let sample : WebPart = path "/hello" >>= choose [ GET >>= request(fun r -> OK <| greetings (query r)) POST >>= request(fun r -> OK <| greetings (form r)) NOT_FOUND "Found no handlers" ] 

Unfortunately, I get a compiler error in the (query r) part:

 error FS0001: Expecting a type supporting the operator '^^' but given a function type. You may be missing an argument to a function. 

I tried to narrow the compiler error to a few simple lines and now this:

 let greetings q = defaultArg (q ^^ "name") "World" |> sprintf "Hello %s" let q (rqst : string) = query rqst let t = greetings q 

And now get the same compiler error in q welcome string. The types in my example above are:

 query: string -> (string -> Choice<'T,string>) -> HttpRequest -> Choice<'T,string> greetings: (string -> (string -> Choice<obj,string>) -> HttpRequest -> Choice<obj, string>) -> string q: string -> ((string -> Choice<'a, string>) -> HttpRequest -> Choice<'a, string>) 

So my types don't match, but I'm not too sure how to get them to match.

Is the example simply outdated? Any ideas how I can get this example to compile and run?

I am launching a Visual Studio 2015 RC build

thanks

+5
source share
1 answer

I am not familiar with Suave.IO, but looking at their source code, it really looks like an old code sample that no longer works. The definition of the query function is as follows:

 let query queryKey f (req : HttpRequest) = req.queryParam queryKey |> Choice.from_option (sprintf "Missing query string key '%s'" queryKey) |> Choice.bind f 

Pay attention to three arguments: you only pass the request, so the return value is not a value (or set), it is a function with two arguments.

The ^^ operator, on the other hand, is used to extract a value from a collection by key.

Looking through the story, it really seems like it's an outdated and really broken way to get a collection of query parameters. The correct way now looks like this:

 GET >>= request(fun r -> OK <| greetings r.query) POST >>= request(fun r -> OK <| greetings r.form) 
+4
source

All Articles