在F#中格式化来自C#的流畅/方法链代码

yan*_*nta 2 f# c#-to-f#

像Ninject这样的api使用流畅的样式apis,例如:

Bind<ISomething>()
.To<Something>()
.WithConstructorArgument("arg1", "somevalue")
.OnActivation(x => x.DoSomething())
Run Code Online (Sandbox Code Playgroud)

当我尝试在F#中格式化这样的代码时,编译器会在方法调用之间的空白处抱怨.

是否可以将方法调用放在单独的行上?我在考虑类似于流水线操作符的操作,但在这种情况下并不完全确定.

如何在F#格式化?

Bri*_*ian 7

你确定这不起作用吗?

Bind<ISomething>() 
 .To<Something>() 
 .WithConstructorArgument("arg1", "somevalue") 
 .OnActivation(fun x -> x.DoSomething()) 
Run Code Online (Sandbox Code Playgroud)

(注意.s 之前的一个空格)

是的,没关系:

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()) 
Run Code Online (Sandbox Code Playgroud)

只要在尝试将单个表达式连续到多行时有一些前导空格,就可以了.

(请注意,除非您使用curried方法参数为其设计API,否则通常不会使用流水线操作.)