Scala“案例类”的等效项在 F# 中
我正在寻找 F# 中 Scala 中可用的“案例类”的等效项。
当您想要使用方法和字段创建自定义类并且仍然能够将它们与模式匹配一起使用时,案例类非常有用,如此 Scala 网站的文章。
有谁知道 F# 中是否存在同样的情况?
I am looking for the equivalent in F# of "case classes" that are available in Scala.
Cases classes are very useful when you want to create custom classes with methods and fields and still be able to use them with pattern matching, as described in this article of the Scala website.
Does anyone know if the same exists in F#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Brian 提到的,模式匹配有两种方法:1. 区分联合和 2. 现有类型上的活动模式。
让我们从这个 Scala 示例开始:
这个 OO 设计可以转换为 F# 中的可区分联合 (DU):
基于此 DU,您可以匹配
Term
值来查找它是什么子类型:请注意,您可以在此
Term
类型上定义方法和属性:现在差异如下:
您无法在其子类型上定义方法:
Var
、Fun,和
应用程序
。您可以在
Term
上定义的方法是不可变的。一旦定义了 DU,就无法对其进行扩展。考虑一下您现在需要向
Term
添加一个For
子类型。然后您必须更改大量Term
模式匹配的代码。而在面向对象设计中,这不是一个问题。因为新的子类型可以携带它自己的实现。
在 F# 中,当您想要在子类型上构建简洁的类型匹配时,应首先考虑 DU。但它也有明显的限制。我认为活动模式匹配更等于Scala中的case类(我只读了一点Scala):
以及使用活动模式的
eval
函数:Activity patten结合了双方的优点:函数式编程和面向对象。
参考号此处和此处了解活动模式。
您可以进一步参考 Don Syme 的有关活动模式的原始论文。
As Brian mentions, there are two ways for pattern matching: 1. Discriminated unions and 2. active pattern on an existing type.
Let's start from this Scala example:
This OO design could be translated to discriminated unions (DU) in F#:
Base on this DU, you can matching a
Term
value to find what subtype it is:Notice that you can have methods and properties defined on this
Term
type:Now here comes the differences:
you cannot define methods on its subtypes:
Var
,Fun
, andApp
.the methods you can define on
Term
are immutable.it is not possible to extend a DU once it is defined. Think about you now need to add a
For
subtype toTerm
. Then you have to change a lot of code where aTerm
is pattern matched.while in oo design, it is less a problem. because the new subtype could carry its own implementations.
In F#, DU should be first considered when you want to build succinct type matching over subtypes. But it also has obvious restrictions. I think activity pattern matching is more equal to the case class in Scala (I only read a little Scala):
and the
eval
function using active pattern:Activity patten combines the good things on both sides: functional programming and object oriented.
ref. here and here for activity patterns.
You can further refer to the original paper on active pattern by Don Syme.
受歧视的工会?您可以向它们添加成员方法。或者,您可以在现有类上使用活动模式。
Discriminated unions? You can add member methods to them. Alternatively you can use active patterns on an existing class.