I think this is somewhat related to this question, but being not sure and since there’s no real answer there, here I go:
in Scala there’s you can write code such as:
aStringArray.map(_.toUpperCase())
which is shorthand for:
aStringArray.map(s => s.toUpperCase())
Is there anything like this in F# or a way to implement it (without the ? operator or heavy use of reflection)? If that’s not possible, is this being considered as a language feature for a future version? (I really the verbosity of functions just to call a method on an object in a closure!).
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 usingfun 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.:
Then you can use both
fun v -> v.Add(10)andFoo.add 10to create a function value. Unfortunately, theStringtype doesn’t have corresponding module with all the functions in the core F# libraries, so you’d have to write it yourself.