enums

How to create an enum and iterate over it

自古美人都是妖i 提交于 2021-02-04 19:59:54
问题 I'm trying to replicate the Java enums in Go. I would like to define an enum and then iterate over it to do some validations. Something like this in java public enum Direction { NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST } And the I would like to iterate over it, like this: for (Direction dir : Direction.values()) { // do what you want } Is there a similar way to achieve this in Golang, I'm thinking in using structs but I don't think it's the best way. Any ideas? 回答1

Check if an enum flag contains a certain flag value

柔情痞子 提交于 2021-02-04 16:32:07
问题 In C# (using Unity working on a game) I have an enum with the [Flag] attribute. I have instantiated it twice. I would like a way to compare the two enums. Specifically if enum A (which will have multiple flags) contains a flag from enum B (which will only ever be assigned a single flag). I am not trying to compare a single instantiated enum to a single flag (this has been answered multiple times). I suspect I could do this by dumping values with GetValue and comparing those values on a

enum valueOf IllegalArgumentException: No enum const class

依然范特西╮ 提交于 2021-02-04 13:37:09
问题 I have used enums in java in the past but for some reason I am getting a strange error right now. the Line of code that it is throwing the error is: switch(ConfigProperties.valueOf(line[0].toLowerCase()){ ... } I am getting a java.lang.IllegalArgumentException: No enum const class allautomator.ConfigProperties.language in the example line is an array of Strings. I am just really confused right now, I do not know what could possibly be wrong. 回答1: The enum constants are case sensitive, so make

How to deserialise enumeration with string representation?

落爺英雄遲暮 提交于 2021-02-04 07:31:30
问题 I would like to create a class, which has an enumeration as an attribute. This enumeration should have a string representation that shows up as human-readable value when dumping the instance of the class that uses the enum attribute as a JSON string. In the minimal working example below, I created three enumerations in three different ways. After the deserialization, each attribute shows us that it comes from an enumeration except the enumeration with a string representation. It is just a

How to deserialise enumeration with string representation?

我的未来我决定 提交于 2021-02-04 07:30:25
问题 I would like to create a class, which has an enumeration as an attribute. This enumeration should have a string representation that shows up as human-readable value when dumping the instance of the class that uses the enum attribute as a JSON string. In the minimal working example below, I created three enumerations in three different ways. After the deserialization, each attribute shows us that it comes from an enumeration except the enumeration with a string representation. It is just a

How to create enum values as type/interface in typescript?

核能气质少年 提交于 2021-02-02 09:56:29
问题 I have enum: enum DemoEnum { a = 'EnumValueA', b = 'EnumValueB' } I would like to create type Type = 'EnumValueA' | 'EnumValueB' from my enum values. How can I do this? My current state is type of "keys": type Type = keyof typeof DemoEnum // 'a' | 'b' For example I would like to use it in my react props. type Props { value: 'EnumValueA' | 'EnumValueB', } In case of usage <MyComponent value='EnumValueA'> type Props { value: DemoEnum, } I am getting an error Type .. is not assignable to

How to create enum values as type/interface in typescript?

我们两清 提交于 2021-02-02 09:54:49
问题 I have enum: enum DemoEnum { a = 'EnumValueA', b = 'EnumValueB' } I would like to create type Type = 'EnumValueA' | 'EnumValueB' from my enum values. How can I do this? My current state is type of "keys": type Type = keyof typeof DemoEnum // 'a' | 'b' For example I would like to use it in my react props. type Props { value: 'EnumValueA' | 'EnumValueB', } In case of usage <MyComponent value='EnumValueA'> type Props { value: DemoEnum, } I am getting an error Type .. is not assignable to

Defining a method for a struct only when a field is a certain enum variant?

别等时光非礼了梦想. 提交于 2021-02-02 09:26:52
问题 I have the following struct: #[derive(Debug)] pub struct Entry { pub index: usize, pub name: String, pub filename_offset: u64, pub entry_type: EntryType, } #[derive(Debug)] pub enum EntryType { File { file_offset: u64, length: usize, }, Directory { parent_index: usize, next_index: usize, }, } Entry is an entry in a GameCube ROM file system table which describes a file or directory. I defined various methods for Entry such as Entry::read_filename and Entry::write_to_disk . However, I have some

Defining a method for a struct only when a field is a certain enum variant?

放肆的年华 提交于 2021-02-02 09:21:45
问题 I have the following struct: #[derive(Debug)] pub struct Entry { pub index: usize, pub name: String, pub filename_offset: u64, pub entry_type: EntryType, } #[derive(Debug)] pub enum EntryType { File { file_offset: u64, length: usize, }, Directory { parent_index: usize, next_index: usize, }, } Entry is an entry in a GameCube ROM file system table which describes a file or directory. I defined various methods for Entry such as Entry::read_filename and Entry::write_to_disk . However, I have some

Why does the size of enum only correspond to the size of a single value?

徘徊边缘 提交于 2021-01-29 20:31:12
问题 This post does not have an answer to my question. Consider this: enum seq {VAL1, VAL2 = 1000000000, VAL3 = UINT_MAX}; int main(void) { printf("%lu\n", sizeof(enum seq)); } Here UINT_MAX is the max value for a uint32_t (4 billion or something) Why is the size of the entire enum type appears to be only 4 bytes? This is only enough to store a single integer value. 回答1: I think maybe I'm starting to understand your question. In your example program, the numbers 0 , 1000000000 and UINT_MAX do not