Pergunta

Some apis like Ninject use fluent style apis, example:

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

When I try format the code like this in F# the compiler complains at the whitespace between method calls.

Is it possible to put the method calls on seperate lines? I was thinking something like the pipelining operator |> but not exactly sure how in this case.

How should this be formatted in F#?

Foi útil?

Solução

Are you sure this doesn't work?

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

(note one space before the .s)

Yeah, it's fine:

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

So long as you have some leading whitespace when you try to continue a single expression onto multiple lines, you're ok.

(Note that pipelining in general won't work unless you have APIs designed for it with curried method parameters.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top