scala和java枚举之间的差异 [英] Differences between scala and java enumerations

查看:151
本文介绍了scala和java枚举之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读有关某人说scala枚举无效的答案,你应该只使用java枚举如果你真的需要它。



虽然我之前使用过java枚举,但我不能说我完全理解它们,因为我在日常工作中不用java编码。



有人可以解释scala和java枚举之间的区别,以及scala枚举中的缺点究竟在哪里?

解决方案

Scala枚举的主要缺点是难以向它们添加方法和字段。在Java中,将枚举用作具有固定数量实例的类是很简单的(例如,为什么它们是实现单例的好选择。)



<但是,在Scala中,Enums只是一组可能的值;向它们添加方法和字段是一个更加hacky的过程。因此,如果你想要从枚举中获得超出微不足道的行为,那么Java枚举是一个更方便的工具。



例如,Java枚举可能如下所示:

  public enum Month {
january(31),
Feb February(28),
...
12月(31);

public final int daysInMonth;
Month(int daysInMonth){
this.daysInMonth = daysInMonth;
}
}

但是,在Scala中,你必须这样做:

  object月份扩展枚举{
受保护的案例类Val(val daysInMonth:Integer)扩展super.Val {}
隐式def valueToMonth(x:Value)= x.asInstanceOf [Val]
val january = Val(31)
val february = Val(28)
....
}

在这样一个简单的例子中,这看起来并不坏,但确实如此添加一些令人困惑的语法,并添加另一个类的开销,需要隐式转换为enum的大多数用法。



我看到Java的主要优点enum是你写的正是你的意思;一个枚举,上面有一些方法和字段。在斯卡拉,你写的不是你的意思;你需要创建一个枚举,它包含一个具有你想要的方法和字段的类,并且还定义了从该类到枚举的转换。这是一种表达相同想法的不那么惯用的方式。



正如所指出的那样在评论中,Scala确实提供了案例类,在许多情况下可以用作枚举的替代品,并且具有更清晰的语法。但是仍然存在一些情况,其中Case Classes是不够的(例如当你想迭代所有值时),所以常规枚举仍然有它们的位置。 更正:使用宏确实可以迭代Case Classes,但这样做有其自身增加的复杂性,而枚举(在Scala和Java中)更容易迭代。


I read an answer on SO where someone said that scala enumerations are useless, and you should just use java enumerations instead if you really need too.

Although I have used java enumerations before, I can't say I fully understand them as I don't code in java during my day job.

Can someone explain the differences between scala and java enumerations, and where exactly the shortcomings in scala enums are?

解决方案

The main weakness of Scala enums is the difficulty in adding methods and fields to them. In Java, it's trivial to use an enum as a Class with a fixed number of instances (which is, for example, why they're a good choice for implementing singletons).

In Scala however, Enums are just a set of possible values; adding methods and fields to them is a much more hacky process. So if you want anything beyond trivial behavior from the enum, Java enums are a much more convenient tool.

For example a Java enum might look like this:

public enum Month{
    january(31),
    february(28),
    ...
    december(31);

    public final int daysInMonth;
    Month(int daysInMonth){
        this.daysInMonth = daysInMonth;
    }
}

However, in Scala, you would have to do this:

object Month extends Enumeration{
    protected case class Val(val daysInMonth:Integer) extends super.Val{}
    implicit def valueToMonth(x:Value) = x.asInstanceOf[Val]
    val january = Val(31)
    val february = Val(28)
    ....
}

This doesn't look that bad in a trivial example like this, but it does add some confusing syntax, and adds the overhead of another class which will need to be implicitly converted to for most uses of the enum.

The main advantage I see to the Java enum is that you write exactly what you mean; an enum, with some methods and fields on it. In scala, you're writing something other than what you mean; you need to create an enum, which contains a class which has the methods and fields you want, and also defines a conversion from that class to the enum. It's a less idiomatic way to express the same idea.

As was pointed out in the comments, Scala does offer Case Classes, which can be used as an alternative to enums in many cases, and have a much cleaner syntax. But there still are some situations in which Case Classes aren't sufficient (such as when you want to iterate over all values), so regular enums still have their place. Correction: using macros does make it possible to iterate over Case Classes, but doing so has its own added complexity, while enums (in both Scala and Java) are much more straightforward to iterate over.

这篇关于scala和java枚举之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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