2010-11-28 43 views
2

像Ninject某些API使用流利式的API,例如:格式化流利/法在F#链接从C#代码

Bind<ISomething>() 
.To<Something>() 
.WithConstructorArgument("arg1", "somevalue") 
.OnActivation(x => x.DoSomething()) 

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

是否可以将方法调用放在单独的行上?我在想像流水线操作符|>,但不完全确定在这种情况下。

这应该如何在F#中格式化?

回答

6

你确定这不起作用?

Bind<ISomething>() 
.To<Something>() 
.WithConstructorArgument("arg1", "somevalue") 
.OnActivation(fun x -> x.DoSomething()) 

(注意.年代以前有一个空格)

是啊,它的罚款:

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()) 

只要你有一些领先的空白,当您试图继续一个表达式到多行,你没事。

(请注意,除非您使用curried方法参数为其设计API,否则一般情况下流水线将不起作用。)