enum

How to compare enum with associated values by ignoring its associated value in Swift?

匿名 (未验证) 提交于 2019-12-03 01:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: After reading How to test equality of Swift enums with associated values , I implemented the following enum: enum CardRank { case Number(Int) case Jack case Queen case King case Ace } func ==(a: CardRank, b: CardRank) -> Bool { switch (a, b) { case (.Number(let a), .Number(let b)) where a == b: return true case (.Jack, .Jack): return true case (.Queen, .Queen): return true case (.King, .King): return true case (.Ace, .Ace): return true default: return false } } The following code works: let card: CardRank = CardRank.Jack if card == CardRank

Ubuntu 16.04, Python 2.7 - ImportError: No module named enum

匿名 (未验证) 提交于 2019-12-03 01:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: First time using Ubuntu. I installed Anaconda 4.1.1 (Python 2.7). I was trying to use enum but I got an import error. import enum Traceback (most recent call last): File "<ipython-input-1-13948d6bb7b8>", line 1, in <module> import enum ImportError: No module named enum I tried using: conda install -c menpo enum=0.4.4 but it didn't work. I also tried installing enum from this link - https://pypi.python.org/pypi/enum34#downloads None of these solutions have worked for me so far. Any help would be greatly appreciated. Thanks! 回答1: Try pip

Why we can&#039;t have “char” enum types

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to know why we can't have "char" as underlying enum type. As we have byte,sbyte,int,uint,long,ulong,short,ushort as underlying enum type. Second what is the default underlying type of an enum? 回答1: The default type is int. More information at the C# reference at MSDN. You can also find a link to the C# language specification at MSDN. I think the reason for the restriction probably derives from these statements in the language specification, section 4.1.5. The char type is classified as an integral type, but it differs from the other

Cannot refer to the static enum field within an initializer?

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I just got Java5 project that has this error, i tried using Java5 and Java6, but its still there. it worked somehow before(since it was in svn), how can i bypass that compiler error? 回答1: Don't "bypass" the error - it won't do what you want it to. The error is there for good reason. The enum values are initialized before any other static fields. If you want to do something like adding all the values into a map, do it in a static initializer after everything else: import java.util.*; public enum Foo { BAR, BAZ; private static final Map

How to compare enum and int values?

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: enum MyEnum { Invalid = 0 , Value1 = 1 , Value1 = 2 , } void main () { MyEnum e1 = MyEnum . Value1 ; int i1 = 2 ; // Is there any difference how to compare enumEration values with integers? if ( e1 ==( MyEnum ) i1 )... // 1st if ( ( int ) e1 == i1 )... // 2nd In each of mentioned cases we have convertion of enum to int or int to enum. Is there any difference in these conversions (performance, any other)? Or they are exactly the same? Thanks. P.S. In current example I compare to 'magic number' but in real application I am getting data from

Java: Check if enum contains a given string?

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Here's my problem...I'm looking for (if it even exists) the enum equivalent of ArrayList.contains();. Here's a sample of my code problem: enum choices { a1 , a2 , b1 , b2 }; if ( choices .???( a1 )}{ //do this } Now, I realize that an ArrayList of Strings would be the better route here but I have to run my enum contents through a switch/case elsewhere. Hence my problem. Assuming something like this doesn't exist, how could I go about doing it? Thanks! 回答1: This should do it: public static boolean contains ( String test ) { for (

Persisting data suited for enums

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Most projects have some sort of data that are essentially static between releases and well-suited for use as an enum, like statuses, transaction types, error codes, etc. For example's sake, I'll just use a common status enum: public enum Status { ACTIVE(10, "Active"); EXPIRED(11, "Expired"); /* other statuses... */ /* constructors, getters, etc. */ } I'd like to know what others do in terms of persistence regarding data like these. I see a few options, each of which have some obvious advantages and disadvantages: Persist the possible

Python Enum class (with tostring fromstring)

匿名 (未验证) 提交于 2019-12-03 01:27:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've found a simply way to implement(hack) an enum into Python: class MyEnum: VAL1, VAL2, VAL3 = range(3) I can then call this as such: bob = MyEnum.VAL1 Sexy! Alright, now I want to be able to get both the numerical value if given a string, or a string if given a numerical value. Let's say I want the strings to exactly match up to the Enum key's The best I could think of is something like this: class MyEnum: VAL1, VAL2, VAL3 = range(3) @classmethod def tostring(cls, val): if (val == cls.VAL1): return "VAL1" elif (val == cls.VAL2): return

Gson: How to change output of Enum

匿名 (未验证) 提交于 2019-12-03 01:26:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I've this enum : enum RequestStatus { OK ( 200 ), NOT_FOUND ( 400 ); private final int code ; RequestStatus ( int code ) { this . code = code ; } public int getCode () { return this . code ; } }; and in my Request-class, I have this field: private RequestStatus status . When using Gson to convert the Java object to JSON the result is like: "status" : "OK" How can I change my GsonBuilder or my Enum object to give me an output like: "status" : { "value" : "OK" , "code" : 200 } 回答1: You can use something like this: GsonBuilder builder

Enums shared static look-up method

匿名 (未验证) 提交于 2019-12-03 01:26:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following Enum: public enum MyEnum{ A(10, "First"), // B(20, "Second"), // C(35, "Other options"); private Integer code; private String description; private MyEnum(Integer code, String description) { this.code = code; this.description = description; } public Integer getCode() { return code; } public String getDescription() { return description; } public static MyEnum getValueOf(Integer code) { for (MyEnum e : MyEnum.values()) { if (e.getCode().equals(code)) { return e; } } throw new IllegalArgumentException("No enum const " +