enums

How to reverse typing on a typescript enum?

心已入冬 提交于 2021-01-29 13:43:27
问题 Given an enum in typescript enum CoffeeSizes { Large = 'L', Medium = 'M', ExtraLarge = 'XL', } CoffeeSizes.Large === 'L' // true How do I do the reverse lookup, where I can say CoffeeSizes.L === 'Large' How can I create a type with the inverse enum? 回答1: Reverse mappings are only possible for numeric non const enums. As in the example below enum CoffeeSizes { Large, Medium, ExtraLarge } const nameOffLargeCoffeeSize = CoffeeSizes[CoffeeSizes.Large]; CoffeeSizes[nameOffLargeCoffeeSize] ===

How to make an enum recognized by other classes

本小妞迷上赌 提交于 2021-01-29 13:35:25
问题 I'm creating an enum called RiskFactor in my SimulationEngine base class. class SimulationEngine { public: enum RiskFactor { interest_rate, equities, volatility }; SimulationEngine(double horizon, Wrapper<valuationFunction>& theFunction_, RiskFactor simulatedRiskFactor); virtual void DoOnePath(double vol, double normvariate) = 0; virtual SimulationEngine* clone() const = 0; const virtual double GetHorizon(); const Wrapper<valuationFunction>& GetFunction() const; RiskFactor simulatedRiskFactor

How can I implement a trait in scope for an enum of existing types for which the trait is implemented?

人走茶凉 提交于 2021-01-29 11:10:03
问题 How can I implement a trait in scope for an enum of existing types for which the trait is implemented? I have this: extern crate pnet; use pnet::packet::ipv4::Ipv4Packet; use pnet::packet::ipv6::Ipv6Packet; enum EthernetType { IPv4, ARP, VLAN, IPv6, Unknown(u16), } enum IPPacket<'a> { IPv4(Ipv4Packet<'a>), IPv6(Ipv6Packet<'a>), } fn ip_decode(pkt: &[u8]) -> IPPacket { let version = (pkt[0] & 0xf0) >> 4; if version == 4 { IPPacket::IPv4(Ipv4Packet::new(&pkt).unwrap()) } else { IPPacket::IPv6

How to access integer and string of enum type?

蹲街弑〆低调 提交于 2021-01-29 04:59:47
问题 What C# code would output the following for a variable of the enum type below? Dentist (2533) public enum eOccupationCode { Butcher = 2531, Baker = 2532, Dentist = 2533, Podiatrist = 2534, Surgeon = 2535, Other = 2539 } 回答1: It sounds like you want something like: // Please drop the "e" prefix... OccupationCode code = OccupationCode.Dentist; string text = string.Format("{0} ({1})", code, (int) code); 回答2: You can also use the format strings g , G , f , F to print the name of the enumeration

How to skip a lot of values when define const variable with iota?

六眼飞鱼酱① 提交于 2021-01-29 04:03:32
问题 Say I have next c program: #include <stdio.h> int main(int args, char* argv[]) { enum RC { APPLE=0, ORANGE, PEAR, BANANA=99, GRAPE }; printf("%d, %d, %d, %d, %d\n", APPLE, ORANGE, PEAR, BANANA, GRAPE); } The output is: 0, 1, 2, 99, 100 If in go, how can I use a more golang way to handle that? In fact, if I just want to skip some value. e.g. print 0, 1, 2, 5, 6 , then I can use next to skip some value, but here I need to skip 96 values... package main import "fmt" func main() { const ( APPLE =

Why Java don't force to use final with enum properties

我们两清 提交于 2021-01-29 02:31:38
问题 Let's take a look with my enum definition here: public enum Day { MONDAY(1), TUESDAY(2), WEDNESDAY(3); int index; Day(int i) { index = i; } public void setIndex(int index) { this.index = index; } public static void main(String[] args) { Day x = MONDAY; Day y = MONDAY; x.setIndex(2); System.out.println(y.index); // Ouput: 2 } In general, I know we should not implement code like that. To prevent this, why java don't use final for final int index like Java treat with interface's properties. Does

How can I use automapper to map 2 enums that use different casing

我与影子孤独终老i 提交于 2021-01-28 20:33:18
问题 I have many enums that I want to keep all caps that I have to map to another system that has no standard at all (caps, no caps, pascal, camel). I can't find an automapper flag to tell it to ignore case for enums. I could use a custome converter for each enum but I would prefer a generic converter since there are so many. Some answers here have implied that automapper does this already. I don't get that from my testing. If I have these enums: public enum AllCaps { VALUE1, VALUE2, VALUE3 }

How to ignore invalid enum values during xml deserialization?

ⅰ亾dé卋堺 提交于 2021-01-28 19:20:51
问题 I want to deserialize an xml document to a class, which is genereated by the concerning xsd files. I don't have control over the contents of the xml-file. During deserialization I run into an exception, because an enum value in the xml document does not meet the requiremnts of the xsd. Instead of breaking, i would like the deserialization to continue and just take the default value for any such errors. Is there any way to accomplish this behaviour? edit: For clarification, what i am trying to

How to ignore invalid enum values during xml deserialization?

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-28 18:50:27
问题 I want to deserialize an xml document to a class, which is genereated by the concerning xsd files. I don't have control over the contents of the xml-file. During deserialization I run into an exception, because an enum value in the xml document does not meet the requiremnts of the xsd. Instead of breaking, i would like the deserialization to continue and just take the default value for any such errors. Is there any way to accomplish this behaviour? edit: For clarification, what i am trying to

How to ignore invalid enum values during xml deserialization?

China☆狼群 提交于 2021-01-28 18:38:44
问题 I want to deserialize an xml document to a class, which is genereated by the concerning xsd files. I don't have control over the contents of the xml-file. During deserialization I run into an exception, because an enum value in the xml document does not meet the requiremnts of the xsd. Instead of breaking, i would like the deserialization to continue and just take the default value for any such errors. Is there any way to accomplish this behaviour? edit: For clarification, what i am trying to