F# Higher-order property accessors

半腔热情 提交于 2019-11-29 15:10:35
Tomas Petricek

I agree it would be nice to have some way of using instance member as a function value in F# (without explicitly constructing the lambda function). This has been actually discussed a few times in the F# community. Here is one related link:

A few suggested options from that discussion are:

// This would turn '_' automatically into a lambda parameter
// (this looks okay in simple cases, but doesn't probably scale well)
examples |> Seq.map (_.Description)

// You would specify instance member using special '#' symbol
examples |> Seq.map (Example#Description)

So, this is something that the F# team is aware of, but I don't think there is any conclusion whether this is actually that important feature and what would be the best way to support it.

examples |> Seq.map (fun e -> e.Description)

(Declaring a record does not create any associated functions, but the record has properties, so a tiny lambda like above makes it easy to project out certain fields.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!