enums

Get the module symbol, given I have the module class, scala macro

瘦欲@ 提交于 2021-01-20 13:08:23
问题 I'm trying to build a simple typeclass IsEnum[T] using a macro. I use knownDirectSubclasses to get all the direct subclasses if T , ensure T is a sealed trait, and that all subclasses are of case objects (using subSymbol.asClass.isModuleClass && subSymbol.asClass.isCaseClass ). Now I'm trying to build a Seq with the case objects referred by the subclasses. It's working, using a workaround: Ident(subSymbol.asInstanceOf[scala.reflect.internal.Symbols#Symbol].sourceModule.asInstanceOf[Symbol])

Get the module symbol, given I have the module class, scala macro

。_饼干妹妹 提交于 2021-01-20 13:02:02
问题 I'm trying to build a simple typeclass IsEnum[T] using a macro. I use knownDirectSubclasses to get all the direct subclasses if T , ensure T is a sealed trait, and that all subclasses are of case objects (using subSymbol.asClass.isModuleClass && subSymbol.asClass.isCaseClass ). Now I'm trying to build a Seq with the case objects referred by the subclasses. It's working, using a workaround: Ident(subSymbol.asInstanceOf[scala.reflect.internal.Symbols#Symbol].sourceModule.asInstanceOf[Symbol])

Get the module symbol, given I have the module class, scala macro

寵の児 提交于 2021-01-20 13:01:09
问题 I'm trying to build a simple typeclass IsEnum[T] using a macro. I use knownDirectSubclasses to get all the direct subclasses if T , ensure T is a sealed trait, and that all subclasses are of case objects (using subSymbol.asClass.isModuleClass && subSymbol.asClass.isCaseClass ). Now I'm trying to build a Seq with the case objects referred by the subclasses. It's working, using a workaround: Ident(subSymbol.asInstanceOf[scala.reflect.internal.Symbols#Symbol].sourceModule.asInstanceOf[Symbol])

Get the module symbol, given I have the module class, scala macro

帅比萌擦擦* 提交于 2021-01-20 13:00:43
问题 I'm trying to build a simple typeclass IsEnum[T] using a macro. I use knownDirectSubclasses to get all the direct subclasses if T , ensure T is a sealed trait, and that all subclasses are of case objects (using subSymbol.asClass.isModuleClass && subSymbol.asClass.isCaseClass ). Now I'm trying to build a Seq with the case objects referred by the subclasses. It's working, using a workaround: Ident(subSymbol.asInstanceOf[scala.reflect.internal.Symbols#Symbol].sourceModule.asInstanceOf[Symbol])

Is there an easier way to make enum constants visible?

徘徊边缘 提交于 2021-01-20 06:08:21
问题 I find myself writing stuff like this: pub enum Player {BLACK, WHITE,} const BLACK: Player = Player::BLACK; const WHITE: Player = Player::WHITE; The reason, of course, being to avoid noise in match expressions and other uses of the constants. Is there an easier way to achieve this? 回答1: Yes, just import the enum variants with the use keyword. pub enum Player { Black, White, } use Player::*; 来源: https://stackoverflow.com/questions/64266286/is-there-an-easier-way-to-make-enum-constants-visible

Any trick to defining an enum as flags/powers of 2 without eventually needing a calculator?

倖福魔咒の 提交于 2021-01-16 05:01:11
问题 I know I can multiply but being the lazy programming I am I do not want to. Has anyone devised some sorcery to auto number the enums as powers of two? Here's the example I have just to make it concrete: [Flags] private enum Targets : uint { None = 0, Campaigns = 1, CampaignGroups = 2, Advertisers = 4, AdvertiserGroups = 8, AffiliateGroups = 16, Affiliates = 32, Creatives = 64, DetailedLeads = 128, DetailedSales = 256, ProgramLeads = 512, CreativeDeployments = 1024, CampaignCategories = 2048,

Any trick to defining an enum as flags/powers of 2 without eventually needing a calculator?

北城余情 提交于 2021-01-16 04:57:25
问题 I know I can multiply but being the lazy programming I am I do not want to. Has anyone devised some sorcery to auto number the enums as powers of two? Here's the example I have just to make it concrete: [Flags] private enum Targets : uint { None = 0, Campaigns = 1, CampaignGroups = 2, Advertisers = 4, AdvertiserGroups = 8, AffiliateGroups = 16, Affiliates = 32, Creatives = 64, DetailedLeads = 128, DetailedSales = 256, ProgramLeads = 512, CreativeDeployments = 1024, CampaignCategories = 2048,

How do you modify default enum (de)serialization for non-annotated enums but retain standard behavior (@JsonProperty/@JsonValue/…) in Jackson?

走远了吗. 提交于 2021-01-05 07:32:17
问题 Currently jackson (uncustomized) serializes enums like this: If there is @JsonProperty or @JsonValue , they are used to derive serialized name Otherwise, standard serialization is carried out: index or toString() might be used (depending on the mapper settings), or .name() is called by default For deserialization, it's like this: If there is @JsonProperty , @JsonValue or @JsonCreator , they influence deserialization Otherwise, standard deserialization is used which mirrors serialization. I'd

Serialization of enum fields in Java

ぃ、小莉子 提交于 2021-01-04 22:12:11
问题 From the Javadoc of ObjectInputStream: Enum constants are deserialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not transmitted. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the static method Enum.valueOf(Class, String) with the enum constant's base type and the

Check if an enum contains more than one flag [duplicate]

和自甴很熟 提交于 2021-01-03 22:15:19
问题 This question already has answers here : How do I check if more than one enum flag is set? (5 answers) Test that only a single bit is set in Flags Enum [duplicate] (1 answer) Closed 29 days ago . I am trying to check if an "enum instance" contains more than one flag. [Flags] public enum Foo { Bar = 1, Far = 2 } var multiState = Foo.Bar | Foo.Far; MoreThanOneFlag(multiState); // True var singleState = Foo.Bar; MoreThanOneFlag(singleState); // False Additionally I really don't wanna use