F# 从C#in F格式化fluent/method链接代码#
一些API(如Ninject)使用流畅风格的API,例如:F# 从C#in F格式化fluent/method链接代码#,f#,c#-to-f#,F#,C# To F#,一些API(如Ninject)使用流畅风格的API,例如: Bind<ISomething>() .To<Something>() .WithConstructorArgument("arg1", "somevalue") .OnActivation(x => x.DoSomething()) Bind() .至() .WithConstructorArgument(“arg1”,“somevalue”) .on激活(x=>x.DoSomething()) 当我
Bind<ISomething>()
.To<Something>()
.WithConstructorArgument("arg1", "somevalue")
.OnActivation(x => x.DoSomething())
Bind()
.至()
.WithConstructorArgument(“arg1”,“somevalue”)
.on激活(x=>x.DoSomething())
当我在F#中尝试这样格式化代码时,编译器会抱怨方法调用之间的空白
是否可以将方法调用放在单独的行上?我在想一些类似于流水线操作员的东西,但不确定在这种情况下是如何实现的
这应该如何用F#格式化?你确定这行不通吗
Bind<ISomething>()
.To<Something>()
.WithConstructorArgument("arg1", "somevalue")
.OnActivation(fun x -> x.DoSomething())
只要在尝试将单个表达式继续到多行时有一些前导空格,就可以了
(请注意,除非为其设计了带有curried方法参数的API,否则流水线一般不会工作。)这当然可以,但我可能会使用curried参数在C#implementation周围创建一个包装器,以便使用F#惯用的流水线。
type ISomething = interface end
type Something = class end
type Foo() =
member this.To<'a>() = this //'
member this.WithConstructorArgument(s1,s2) = this
member this.OnActivation(x:Foo->unit) = this
member this.DoSomething() = ()
let Bind<'a>() = new Foo() //'
let r =
Bind<ISomething>()
.To<Something>()
.WithConstructorArgument("arg1", "somevalue")
.OnActivation(fun x -> x.DoSomething())