enums

Representing enum variants with optional data in macro_rules

只谈情不闲聊 提交于 2021-02-10 00:26:55
问题 I'm trying to create a macro to help with some boilerplate enum code that I've been repetitively writing. I managed to implement a simple enum (i.e. no arguments) relatively easily using a basic macro_rule . e.g. An excerpt: macro_rules! enum_helper { ($type:ident, { $( $name:ident ), * }) => { enum $type { $( $name, )+ } impl FromSql for $type { fn from_sql<R: Read>(_: &Type, raw: &mut R, _: &SessionInfo) -> Result<&type> { // ... the implementation } // ... plus some other internal traits

Representing enum variants with optional data in macro_rules

做~自己de王妃 提交于 2021-02-10 00:26:18
问题 I'm trying to create a macro to help with some boilerplate enum code that I've been repetitively writing. I managed to implement a simple enum (i.e. no arguments) relatively easily using a basic macro_rule . e.g. An excerpt: macro_rules! enum_helper { ($type:ident, { $( $name:ident ), * }) => { enum $type { $( $name, )+ } impl FromSql for $type { fn from_sql<R: Read>(_: &Type, raw: &mut R, _: &SessionInfo) -> Result<&type> { // ... the implementation } // ... plus some other internal traits

how to (correctly) use an enumerated type with livebindings (TObjectBindSourceAdapter)

此生再无相见时 提交于 2021-02-09 11:01:51
问题 I'm using TObjectBindSourceAdapter to use livebindings with an object. One of the properties of the object i'm using with TObjectBindSourceAdapter has an enumerated type, but the field in the adapter is never generated when i use an enumerated type in my object The Only solution i have found for now is to define the enumerated type as an integer in my object and typecast it. This seems to work fine but you have to keep type casting from and back the enumerated type and integers. Here is some

How to serialise Enums as both Object Shape and default string?

穿精又带淫゛_ 提交于 2021-02-09 07:53:28
问题 For an enum with attributes, eg: public enum Thing { THING_A("a"), THING_B("b"); private String thing; private Thing(String thing) { this.thing = thing; } // Getters... } Jackson serializes as the name of the values, eg: mapper.writeValueAsString(Thing.THING_A)); // "THING_A" If we add the annotation to treat serialisation as an object: @JsonFormat(shape = JsonFormat.Shape.OBJECT) it will serialize the attributes: mapper.writeValueAsString(Thing.THING_A)); // "{"thing":"a"}" I'd like to be

How to convert a string to a enum?

南楼画角 提交于 2021-02-09 05:43:51
问题 I'm trying to convert a string to a enum value in PowerShell but couldn't find this anywhere... I'm getting a JSON result, where I only want to consume the Healthstate which is defined as a string. enum HealthState { Invalid = 0 Ok = 1 Warning = 2 Error = 3 Unknown = 65535 } $jsonResult = "Ok" $HealthStateResultEnum = [Enum]::ToObject([HealthState], $jsonResult) Thanks in advance. 回答1: You can simply cast the string result as the Enum type: $HealthStateResultEnum = [HealthState]$jsonResult

How to convert a string to a enum?

∥☆過路亽.° 提交于 2021-02-09 05:43:33
问题 I'm trying to convert a string to a enum value in PowerShell but couldn't find this anywhere... I'm getting a JSON result, where I only want to consume the Healthstate which is defined as a string. enum HealthState { Invalid = 0 Ok = 1 Warning = 2 Error = 3 Unknown = 65535 } $jsonResult = "Ok" $HealthStateResultEnum = [Enum]::ToObject([HealthState], $jsonResult) Thanks in advance. 回答1: You can simply cast the string result as the Enum type: $HealthStateResultEnum = [HealthState]$jsonResult

Enum.GetName vs Enum.ToString

别等时光非礼了梦想. 提交于 2021-02-08 15:11:23
问题 For this enumeration, Enum MyEnum Value End Enum there are two methods to get the name representation Value of MyEnum.Value : [Enum].GetName(GetType(MyEnum), MyEnum.Value) ' aka Enum.GetName and Dim a As MyEnum = MyEnum.Value a.ToString ' aka Enum.ToString What are their pros and cons? And which is better after all? PS: There is one answer for java but this is .NET which may have different functionality. 回答1: Here are some examples of what can be done using this enum, note the usage of the

Enum.GetName vs Enum.ToString

六眼飞鱼酱① 提交于 2021-02-08 15:10:49
问题 For this enumeration, Enum MyEnum Value End Enum there are two methods to get the name representation Value of MyEnum.Value : [Enum].GetName(GetType(MyEnum), MyEnum.Value) ' aka Enum.GetName and Dim a As MyEnum = MyEnum.Value a.ToString ' aka Enum.ToString What are their pros and cons? And which is better after all? PS: There is one answer for java but this is .NET which may have different functionality. 回答1: Here are some examples of what can be done using this enum, note the usage of the

c# enum covariance doesn't work

廉价感情. 提交于 2021-02-08 13:59:16
问题 I need to use enum as covariant type. Let's say i have this code: public enum EnumColor { Blue = 0, Red = 1, } public class Car : IColoredObject<EnumColor> { private EnumColor m_Color; public EnumColor Color { get { return m_Color; } set { m_Color = value; } } public Car() { } } class Program { static void Main() { Car car = new Car(); IndependentClass.DoesItWork( car ); } } and this code: public interface IColoredObject<out EnumColorType> { EnumColorType Color { get; } } public static class

Set MathContext to BinaryOperator reference methods

霸气de小男生 提交于 2021-02-08 04:27:10
问题 I have this enum: public enum Operator { add("+", BigDecimal::add), subtract("-", BigDecimal::subtract), multiply("*", BigDecimal::multiply), divide("/", BigDecimal::divide), mod("%", BigDecimal::remainder); Operator(final String symbol, final BinaryOperator<BigDecimal> operation) { this.symbol = symbol; this.operation = operation; } public BinaryOperator<BigDecimal> getOperation() { return operation; } } I want to use the some MathContext , one can easily do that when performing an operation