我正试图从Suave.io编译一个简单的F#项目这个例子: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" ]
Run Code Online (Sandbox Code Playgroud)
不幸的是我在(查询r)部分遇到编译器错误:
error FS0001: Expecting a type supporting the operator '^^' but given a function type. You may be missing an argument to a function.
Run Code Online (Sandbox Code Playgroud)
我试图将编译器错误缩小到几个简单的行,现在有了这个:
let greetings q =
defaultArg (q ^^ "name") "World" |> sprintf "Hello %s"
let q (rqst : string) = query rqst
let t = greetings q
Run Code Online (Sandbox Code Playgroud)
现在在问候q行上得到相同的编译器错误.我上面例子中的类型是:
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>)
Run Code Online (Sandbox Code Playgroud)
所以,我的类型不匹配,但我不太清楚我是如何得到这些匹配的.
这个例子刚刚过时了吗?任何想法如何让这个例子编译和运行?
我正在运行Visual Studio 2015的RC版本
谢谢
我对 Suave.IO 不熟悉,但是看他们的源代码,它确实看起来像一个不再工作的旧示例代码。函数的定义query如下:
let query queryKey f (req : HttpRequest) =
req.queryParam queryKey
|> Choice.from_option (sprintf "Missing query string key '%s'" queryKey)
|> Choice.bind f
Run Code Online (Sandbox Code Playgroud)
请注意这三个参数 - 您只是传递请求,因此返回值不是值(或集合),而是带有两个参数的函数。
另一方面,运算符^^用于通过键从集合中检索值。
纵观历史,这似乎确实是一种过时且实际上已损坏的检索查询参数集合的方法。现在看来正确的方法是这样的:
GET >>= request(fun r -> OK <| greetings r.query)
POST >>= request(fun r -> OK <| greetings r.form)
Run Code Online (Sandbox Code Playgroud)