我应该更喜欢在Scala中密封类或枚举? [英] Should I prefer sealed class or Enumeration in Scala?

查看:113
本文介绍了我应该更喜欢在Scala中密封类或枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施适用于Scala的 Plurk API ,但我有一个设计选择



例如,Plurk用户的gender属性可以是male,female,other之一。

 密封特征性别
对象男性扩展性别
对象女性扩展性别
对象其他扩展性别

问题是我应该喜欢密码类/对象像上面的代码示例,超过枚举来表示枚举类型?



因为我发现当我使用其他Scala库时,我很少遇到枚举,并且许多Actor教程使用密封类/对象来表示Actor的消息。 p>

这是否意味着在Scala中,密封类是枚举的更好/常规的选择?

解决方案

如果您不需要迭代您的Gender对象或需要方法,例如 next previous ,类和对象是正确的解决方案。



如果您需要类和对象和scala.Enumeration的功能 thisthis 这个问题可能对你有兴趣。


I'm trying to implement a Plurk API biding for Scala, but I have a design choice to made.

For example, the gender attribute of a Plurk user could be one of "male", "female", "other".

sealed trait Gender
object Male extends Gender
object Female extends Gender
object Others extends Gender

The question is should I prefer sealed class/object like above code example, over Enumeration to represent enumeration types?

Because I find that I rarely come across Enumeration when I use other Scala library, and lots of tutorial of Actor use sealed class/object to represent messages for Actor.

So does this means that in Scala, sealed class is a better/conventional choice over Enumeration?

解决方案

If you don't need to iterate over your objects of Gender or need methods like next and previous, classes and objects is the right solution.

If you need the features of the "classes and objects" and of "scala.Enumeration" this and this question could be of interest to you.

这篇关于我应该更喜欢在Scala中密封类或枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆