开发者

F# shorthand to call method on object in lambda

开发者 https://www.devze.com 2023-01-18 05:29 出处:网络
I think this is somewhat related to this question, but being not sure and sinc开发者_运维技巧e there\'s no real answer there, here I go:

I think this is somewhat related to this question, but being not sure and sinc开发者_运维技巧e 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 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.


Just use fun s -> s.ToUpper() as the parameter, which creates the same kind of anonymous function Scala does.

Note that this nifty _ trick from Scala doesn't (currently) exist in F#, but if you weren't calling an object method, you could just use partial evaluation like in

filter ((=) 42) list


See this forum thread:

http://cs.hubfs.net/forums/permalink/13313/13319/ShowThread.aspx#13319

The F# team has been over this territory a number of times, but nothing workable has ever come of it. At this point I think it's doubtful we'll introduce a new syntactic form that saves less than 10 characters.


This isn't really the answer you wanted, but currently I work around this by creating dummy static functions (if I'm going to be needing the same thing several times), as it's prettier than the lambda syntax:

let toupper (s:string) = s.ToUpper()
aStringArray.map toupper
0

精彩评论

暂无评论...
验证码 换一张
取 消