如果我定义一个简单的函数:
let myConcat a b =
a + "+" + b
Run Code Online (Sandbox Code Playgroud)
然后假设函数是F#中的第一类值,我希望能够myConcat
像这样使用:
let result = myConcat "a" (fun () -> "b")
Run Code Online (Sandbox Code Playgroud)
它没有产生字符串"a + b",而是给出了以下错误:
error FS0002: This function takes too many arguments, or is used in a context where a function is not expected
Run Code Online (Sandbox Code Playgroud)
希望我只是让语法错误,但在我看来,函数不能真正用作F#中的值.谁能解释一下这里发生了什么?
编辑 为了进一步澄清我的要求,我可以拥有等效的C#代码:
public string myConcat(string a, string b) { return a + "+" + b; }
Run Code Online (Sandbox Code Playgroud)
如果我想在param b中传递"稍后调用函数",我必须这样做:
public string myConcat(string a, Action<string> b) { return a + "+" + b(); }
Run Code Online (Sandbox Code Playgroud)
或者我可以这样称呼它: …