我该如何在 F# 中执行此操作 (C#)
public class MyClass
{
void Render(TextWriter textWriter)
{
Tag(() =>
{
textWriter.WriteLine("line 1");
textWriter.WriteLine("line 2");
});
Tag(value =>
{
textWriter.WriteLine("line 1");
textWriter.WriteLine(value);
}, "a");
}
public void Tag(Action action)
{
action();
}
public void Tag<T>(Action<T> action, T t)
{
action(t);
}
}
F# 中的多行 lambda 只是
(fun args ->
lots
of
code
here
)
整个代码就像这样
open System.IO
type MyClass() as this =
let Render(tw : TextWriter) =
this.Tag(fun() ->
tw.WriteLine("line1")
tw.WriteLine("line2")
)
this.Tag(fun(value : string) ->
tw.WriteLine("line1")
tw.WriteLine(value)
, "a"
)
member this.Tag(action) =
action()
member this.Tag(action, x) =
action(x)
假设我没有犯抄写错误。 (我在公共接口中使用了 F# 函数类型而不是 Action 委托。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)