对Scala Enumerations的懒惰评估?

时间:2015-11-13 10:24:13

标签: scala object enumeration

请考虑以下代码段,其中按字母顺序排列从1到9的所有数字:

object AlphabetizedDigit extends Enumeration {
    type AlphabetizedDigit = Value
    val one, two, three, four, five, six, seven, eight, nine = Value
}

println(for(i <- Range(0, AlphabetizedDigit.maxId)) yield (i, AlphabetizedDigit(i)))

由于零索引,println的输出有点不直观:

jason@debian:~/code/atomicscala$ scala EnumIDRange.scala 
Vector((0,one), (1,two), (2,three), (3,four), (4,five), (5,six), (6,seven), (7,eight), (8,nine))

解决此问题的一种方法是更改​​AlphabetizedDigit成员的定义,如下所示:

val one = Value(1)
val two, three, four, five, six, seven, eight, nine = Value

然后确保Range1而不是0开始。然后输出是直观的:

jason@debian:~/code/atomicscala$ scala EnumIDRange.scala 
Vector((1,one), (2,two), (3,three), (4,four), (5,five), (6,six), (7,seven), (8,eight), (9,nine))

但是,如果你只想切换枚举的起始索引,我真的不喜欢你需要将声明分成两行。因此,我尝试了以下,编译得很好:

val one, two, three, four, five, six, seven, eight, nine = Value(1)

但是,在运行时,由于使用了重复的ID,因此引发了AssertionError。对我来说很有意义,这不会起作用,但我很好奇为什么在运行时而不是编译时抛出这个错误。

2 个答案:

答案 0 :(得分:5)

您可以使用Enumeration的第二个构造函数,例如:

object AlphabetizedDigit extends Enumeration(1) {
    type AlphabetizedDigit = Value
    val one, two, three, four, five, six, seven, eight, nine = Value
}

表格doc:new Enumeration(initial: Int)

答案 1 :(得分:3)

如果查看the source code for Scala's Enumeration,您会看到以下内容:

public static boolean transform(Logger logger, String inXML,String inXSL,String outTXT)  throws Exception
{  
    try
    {
        TransformerFactory factory = TransformerFactory.newInstance();
        ErrorListener listener = new ErrorListener() 
        {
            @Override
            public void warning(TransformerException exception)
                    throws TransformerException {}
            @Override
            public void fatalError(TransformerException exception)
                    throws TransformerException {}
            @Override
            public void error(TransformerException exception)
                    throws TransformerException {}
        };
        factory.setErrorListener(listener);
        StreamSource xslStream = new StreamSource(inXSL);
        Transformer transformer = factory.newTransformer(xslStream);
        StreamSource in = new StreamSource(inXML); 
        StreamResult out = new StreamResult(outTXT);
        transformer.transform(in,out);
        return true;
    }
    catch(Exception e)
    {
        logger.log("ERROR DURING XSLT TRANSFORM (" + e.getMessage() + ")",2);
        return false;
    }   
} 

/** The cache listing all values of this enumeration. */
@transient private var vset: ValueSet = null
@transient @volatile private var vsetDefined = false

因此,似乎直到需要时才会实际评估值集,因此错误仅在运行时显示。这似乎是实现/** The values of this enumeration as a set. */ def values: ValueSet = { if (!vsetDefined) { vset = (ValueSet.newBuilder ++= vmap.values).result() vsetDefined = true } vset } 的一种不好的方式,但我通常更喜欢使用密封的特征和案例类和对象,因此很少处理Enumeration无论如何*耸肩*。

至于Enumeration的索引问题,为什么不添加0条目?

zero

如果你愿意,你仍然可以从1开始计算:

val zero, one, two, three, four, five, six, seven, eight, nine = Value

或从0开始计算并获得全部十位数字:

scala> println(for(i <- Range(1, AlphabetizedDigit.maxId)) yield (i, AlphabetizedDigit(i)))
Vector((1,one), (2,two), (3,three), (4,four), (5,five), (6,six), (7,seven), (8,eight), (9,nine))