As Dario points out, a feature like _.Foo()
syntax in Scala is not currently available in F#, so you'll have to write a lambda function explicitly using fun a -> a.Foo()
.
As far as I know, a question like this appears every now and then on F# discussions, so it was surely considered by the F# team. It is a bit tricky (e.g. do you want to allow just member uses or other uses e.g. _ + 10
, and what would be the scope of the lambda expression?) Also, the value of the feature is relatively low compared to other things that could be done... Anyway, it would be nice to have it!
This may be also a reason why many F# types expose operations as both members (usable in the OO style) and functions in a module (for the functional style). I think you could consider it as a good F# practice (but it depends on your design preferences). E.g.:
type Foo(a:int) =
member x.Add(b:int) = a + b
// Attribute allows you to define module with the same name
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Foo =
let add b (v:Foo) = v.Add(b)
Then you can use both fun v -> v.Add(10)
and Foo.add 10
to create a function value. Unfortunately, the String
type doesn't have corresponding module with all the functions in the core F# libraries, so you'd have to write it yourself.