开发者

Should I prefer sealed class or Enumeration in Scala?

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 Enumerati开发者_如何学JAVAon?


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.


Using Enumeration is also quite a bit cheaper in terms of class count. In the Gender example that does not matter, but it might start to matter if you want to model processor instructions by an enumeration with 100+ elements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜