如何检查两个值是否是使用同一个构造函数创建的?
假设我有
type t = A of int | B of int
let xx = A(2);;
let yy = A(3);;
并且我想测试 xx 和 yy 的构造函数是否相等, 有没有一种简单的方法可以做到这一点? 不必这样做会变得非常混乱
match xx with
A _ ->
(match yy with A _ -> true | B _ -> false)
| B _ ->
(match yy with A _ -> false | B _ -> true);;
当类型上有很多构造函数时,
let's say I have
type t = A of int | B of int
let xx = A(2);;
let yy = A(3);;
and I want to test if the constructors of xx and yy are equal,
is there an easy way to do this ? Instead of having to
match xx with
A _ ->
(match yy with A _ -> true | B _ -> false)
| B _ ->
(match yy with A _ -> false | B _ -> true);;
which gets quite messy when there many constructors on a type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将上面的内容重写为更简单一些:
但我不知道不枚举所有构造函数的解决方案。
You can rewrite the above to, somewhat simpler:
but I'm not aware of a solution without enumerating all the constructors.
这在某种程度上可以通过
Obj
模块实现。通过Obj
函数分析对象,如果操作正确,不会使程序崩溃;但如果你想获得有意义的结果,你需要小心。当对变体类型(不是多态变体类型)的值进行调用时,如果两个值都具有相同的零参数构造函数或都具有相同的 1-or-more-,则此函数返回
true
参数构造函数,否则为false
。类型系统不会阻止您在其他类型上实例化equal_constructors
;您将得到一个true
或false
返回值,但不一定是有意义的。This is possible, sort of, through the
Obj
module. Analyzing objects through theObj
functions, if done properly, won't crash your program; but you need to be careful if you want to get meaningful results.When called on values of a variant type (not a polymorphic variant type), this function returns
true
if the two values both have the same zero-argument constructor or both have the same 1-or-more-argument constructor, andfalse
otherwise. The type system won't prevent you from instanciatingequal_constructors
at other types; you'll get atrue
orfalse
return value but not necessarily a meaningful one.另一种有效的方法是创建与标签相对应的另一种类型,并使用该类型。
Another way of doing this that can work well is to create another type that corresponds to the tags, and use that type.