fah*_*ash 2 f# c#-to-f# unit-type
我正在寻找一种在F#中定义无参数lambda表达式的方法,就像下面的C#示例一样.
var task = () => {
int x = 3;
DoSomething(x);
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下内容
let task = fun _ ->
let x = 3
doSomething x
Run Code Online (Sandbox Code Playgroud)
它编译但它给task : ('a -> unit)了我我真正想要的是task : (unit -> unit)
在MSDN文档不谈论这个.我在这里错过了什么?
Car*_*ten 13
只是
let task = fun () -> // whatever you need
Run Code Online (Sandbox Code Playgroud)
你的例子是:
let task = fun () ->
let x = 3
DoSomething(3)
Run Code Online (Sandbox Code Playgroud)
假设DoSomething是类型int -> unit- 如果它返回你需要的其他东西
let task = fun () ->
let x = 3
DoSomething(3) |> ignore
Run Code Online (Sandbox Code Playgroud)
得到类型 unit -> unit
备注:
通常你不会写,let task = fun () -> ...而只是let task() = ...
你错过的东西:
如果你写的fun _ -> ()你说你想要一些你不介意的参数 - 所以F#将采取最一般的(在'a这里命名) - 这将包括unit!
()是类型的唯一值unit(或多或少void来自C#...但是F#中的真实类型)
| 归档时间: |
|
| 查看次数: |
506 次 |
| 最近记录: |