Is there a way to specify named arguments for a function in the F # constructor?

Is there a way to name function arguments in a constructor?

type UnnamedInCtor(foo: string -> string -> bool) = 
    member this.Foo: string -> string -> bool = foo
    member this.Bar: a:string -> b:string -> bool = foo
    member this.Fizz = foo

//Does not compile
type NamedInCtor(foo: a:string -> b:string -> bool) = 
    member this.Foo: string -> string -> bool = foo
    member this.Bar: a:string -> b:string -> bool = foo
    member this.Fizz = foo
+4
source share
2 answers

I think this is not possible in F #, however you can use the type of abbreviations if you want to document what foo represents:

// Compiles
type aToBToC = string -> string -> bool
type NamedInCtor(foo: aToBToC) = 
    member this.Foo: string -> string -> bool = foo
    member this.Bar: a:string -> b:string -> bool = foo
    member this.Fizz = foo
+1
source

You will need to remove the function in your constructor:

type NamedInCtor(a, b) = 
    member this.Foo: string -> string -> bool = a b
    member this.Bar: string -> string -> bool = a b
    member this.Fizz = a b

Note that a and b are implicitly entered here. You must trust the compiler to do this as much as possible, because it makes your code more readable.

, - , . , , , " ?" - . , , .

+1

All Articles