与可区分联合相比,在 F# 中使用记录类型时更加详细
let Method = { Name:string } //oversimplification
let method_parser =
spaces >>. many1Satisfy isLetter .>> spaces
|>> (fun name -> { Name=name })
如果我选择使用方法区分联合,事情会更简洁一些:
let method_parser =
spaces >>. many1Satisfy isLetter .>> spaces
|>> Method
我相信在 F# 中使用记录类型时没有办法避免这种冗长的情况。我说得对吗?
let Method = { Name:string } //oversimplification
let method_parser =
spaces >>. many1Satisfy isLetter .>> spaces
|>> (fun name -> { Name=name })
Had I chosen to use a Method discriminated union, instead, and things would be a bit more succinct:
let method_parser =
spaces >>. many1Satisfy isLetter .>> spaces
|>> Method
I believe there is no way to avoid this kind of verbosity when using record types in F#. Am I right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
记录与只有一个案例的受歧视联盟非常相似。在某些情况下,我也更喜欢联合,因为它更容易使用。然而,记录有两个主要优点:
它们为字段命名,这在一定程度上导致了冗长,但使代码更加不言自明
如果字段数量较少,可以使用元组或单例联合。
它们允许您使用
{ info with Name = "Tomas" }
语法来克隆记录如果您不需要这个,您可以使用标准 F# 类(具有更多 .NET 风格)
如果你想获得记录的好处,但仍然使用简单的语法来创建,那么你可以定义一个静态成员来构造你的记录:
然后你可以写:
A record is very similar to a discriminated union with just a single case. In some cases, I would also prefer a union, because it is easier to use. However, records have two main advantages:
They give names to the fields, which partly causes the verbosity, but makes code more self-explanatory
If you have small number of fields, you can use tuple or single-case union.
They allow you to use the
{ info with Name = "Tomas" }
syntax for cloning recordsIf you don't need this, you can use standard F# classes (which have more .NET feel)
If you want to get the benefits of records, but still use simple syntax for creating then, then you can define a static member for constructing your record:
Then you can write: