简体   繁体   English

是否可以将F#record的标签用作Haskell中的函数或类似的东西?

[英]Is it possible to use F# record's labels as functions like in Haskell, or something similar?

In Haskell, given this record: 在Haskell中,给出了这个记录:

data ARecord { labelA :: String, labelB :: Int }

we get this functions: 我们得到这个功能:

labelA :: ARecord -> String
labelB :: ARecord -> Int

F# doesn't seem to work this way. F#似乎没有这种方式。 But, is there something similar? 但是,有类似的东西吗?

Edit 编辑

By something similar I mean something that saves me from having to define the functions manually, as @kaefer suggested. 通过类似的东西,我的意思是让我不必手动定义函数,正如@kaefer建议的那样。

It's easily defined. 它很容易定义。

type ARecord = { labelA : string; labelB : int }
let labelA { labelA = s } = s
// val labelA : ARecord -> string

Edit 编辑

The following function would compile to identical IL, with direct read access to the backing field, instead of the automatically generated instance property. 以下函数将编译为相同的IL,具有对备份字段的直接读取访问权限,而不是自动生成的实例属性。 In contrast to normal experience with object-orientated dot-notation , it doesn't require type annotation to determine the record type. 与使用面向对象的点符号的常规体验相比,它不需要类型注释来确定记录类型。

let labelA' aRecord = aRecord.labelA
// val labelA' : aRecord:ARecord -> string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM