我将如何在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委托。)

关于f# - 如何在F#中执行多行Lambda表达式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/814278/

10-09 02:53