如何在scala中创建一个额外的字段的枚举 [英] How do I create an enum in scala that has an extra field

查看:115
本文介绍了如何在scala中创建一个额外的字段的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public enum FlatFileHeaderMapping {

HEADER_EL(1) ,
HEADER_RESERVED1(5),
HEADER_RESERVED2(2),
HEADER_MESSAGE_TYPE(4)

public final int fieldSize;

private FlatFileHeaderMapping(int fieldSize){
this.fieldSize = fieldSize;
}

}

然后我可以使用它每行进入地图,然后通过此枚举访问地图中的键(如符号)



枚举没有我能看到的这个质量,而案例类不像枚举声明那样排序 - 所以不能用于匹配如上所示的记录布局。至少没有没有一个有序的收藏的支持。



我可能会缺少一些明显的东西,因此这个问题!



谢谢



Ray

解决方案

:/ 7098500142 / diy-exhaustive-checking-on-scala-enums


In Java I have something like this

public enum FlatFileHeaderMapping {

   HEADER_EL(1),
   HEADER_RESERVED1(5),
   HEADER_RESERVED2(2),
   HEADER_MESSAGE_TYPE(4)

   public final int fieldSize;

    private FlatFileHeaderMapping(int fieldSize) {
        this.fieldSize = fieldSize;
   }

}

which I can then use it place each line into a map and later access the keys in the map via this enum (like symbols)

Enumeration does not have this quality as far as I can see, and case classes are not ordered like the enum declarations - so cannot be used to match a record layout as shown above. At least not without the support of an ordered collection.

I could be missing something obvious, hence the question!

Thanks

Ray

解决方案

Here is an approach that works: http://klangism.tumblr.com/post/7098500142/diy-exhaustiveness-checking-on-scala-enums

这篇关于如何在scala中创建一个额外的字段的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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