enums

How can I determine if one Enum value is the successor of another?

╄→尐↘猪︶ㄣ 提交于 2020-05-28 20:49:10
问题 I'm trying to write a function that tells me whether one Enum is the successor of another. Here was my first attempt: isSuccessorOf x y = x == succ y Looks reasonable. Let's try it: λ> isSuccessorOf 3 2 True λ> isSuccessorOf 1 5 False λ> isSuccessorOf 3 (maxBound :: Int) *** Exception: Prelude.Enum.succ{Int}: tried to take `succ' of maxBound Whoops. That should have been False . Let's make sure we don't try to do succ maxBound : isSuccessorOf x y = y /= maxBound && x == succ y Let's try it

C# Enum.HasFlag vs. Bitwise AND Operator Check

不想你离开。 提交于 2020-05-27 15:39:32
问题 If you have an enum that is used for bit flags, i.e., [Flags] internal enum _flagsEnum : byte { None = 0, //00000000 Option1 = 1, //00000001 Option2 = 1 << 1, //00000010 Option3 = 1 << 2, //00000100 Option4 = 1 << 3, //00001000 Option5 = 1 << 4, //00010000 Option6 = 1 << 5, //00100000 Option7 = 1 << 6, //01000000 Option8 = 1 << 7, //10000000 All = Byte.MaxValue,//11111111 } _flagsEnum myFlagsEnum = _flagsEnum.None; Is it faster to do.. bool hasFlag = myFlagsEnum.HasFlag(_flagsEnum.Option1);

Force derived class to implement interface

本秂侑毒 提交于 2020-05-27 03:26:10
问题 I am here today (like yesterday) with another weird interface question. I have a class: public class InputDevice<T> where T : Button { protected List<T> buttons = new List<T>(); protected InputDevice() { //Only allow instanciation from within a derived class } } As you can see, this class cannot be instantiated. A class that derives from it might be able to be instantiated. This is a subclass: public sealed class Keyboard : InputDevice<KeyboardButton> { public Keyboard() { buttons.Add(new

Why does “can't leak private type” only apply to structs and not enums?

╄→гoц情女王★ 提交于 2020-05-22 08:03:11
问题 In Learning Rust With Entirely Too Many Linked Lists, they show that a pub enum can't hold a private struct :, struct Node { elem: i32, next: List, } pub enum List { Empty, More(Box<Node>), } This will cause the compiler to complain: error[E0446]: private type `Node` in public interface --> src/main.rs:8:10 | 8 | More(Box<Node>), | ^^^^^^^^^^ can't leak private type But this code will not cause an error even though Link is private: pub struct List { head: Link, } enum Link { Empty, More(Box

How do I conditionally check if an enum is one variant or another?

僤鯓⒐⒋嵵緔 提交于 2020-05-15 10:37:06
问题 I have an enum with two variants: enum DatabaseType { Memory, RocksDB, } What do I need in order to make a conditional if inside a function that checks if an argument is DatabaseType::Memory or DatabaseType::RocksDB ? fn initialize(datastore: DatabaseType) -> Result<V, E> { if /* Memory */ { //.......... } else if /* RocksDB */ { //.......... } } 回答1: First, go back and re-read the free, official Rust book : The Rust Programming Language, specifically the chapter on enums. match fn initialize

Overloading cast operator for enum class

荒凉一梦 提交于 2020-05-14 20:53:08
问题 In my project, I'm using multiple enum classes which I need to easily cast between depending on where I need to use them. They basically describe the same thing but are named differently to keep the code more easier to work with. Here are the enum classes: enum class tetroType { None, I, O, T, J, L, S, Z }; enum class gridBlock { Empty, Blue, Green, Orange, Purple, Red, Teal, Yellow }; Each value in tetroType corresponds to a value in gridBlock (f. e. tetroType::I = gridBlock::Teal), but the

Python Enums with duplicate values

假如想象 提交于 2020-05-14 19:57:08
问题 I'm having trouble working with an Enum where some attributes have the same value. I think Enums are so new to python that I can't find any other reference to this issue. In any case, let's say I have the following class CardNumber(Enum): ACE = 11 TWO = 2 THREE = 3 FOUR = 4 FIVE = 5 SIX = 6 SEVEN = 7 EIGHT = 8 NINE = 9 TEN = 10 JACK = 10 QUEEN = 10 KING = 10 Clearly these are the card numbers and their corresponding values in black jack. The ten through king have the same value. But if I do

Why doesn't array conform to Equatable, when its items are Equatable in Swift?

梦想与她 提交于 2020-05-14 17:46:07
问题 UPDATE: As of Xcode 9.3, which includes Swift 4.1 , the array equality works as expected , and the code in the original question compiles without errors. However, please see the accepted answer , because it provides a better, more modern solution. The original question is below: When I try to declare an instance of a generic enum with type [Post] , I get an error saying Type '[Post]' does not conform to protocol 'Equatable' which is nonsense, because Post conforms to Equatable and I can

c++: ensure enum values are unique at compile time

怎甘沉沦 提交于 2020-05-13 05:25:23
问题 I have the following enum that describes error codes: typedef enum { et_general = 0, et_INVALID_CLI_FLAG = 1, ... et_undef = 500 } EErrorType; The main reason why I explicitly write the enum values, is to ease the debug process. Anyways, I wonder if there's a way, to make the compiler complain about non unique values . I can always check it at run time easily, but I'd like to avoid that. I've read this post and reviewed this answer. As I understand, that answer suggests to generate the enum

Enum-aware ServiceLoader implementation?

时光毁灭记忆、已成空白 提交于 2020-05-12 07:01:13
问题 I would like to be able to indicate an enum type as an interface implementation and then load all enums as separate instances/implementations of the interface via the ServiceLoader API. An example of this use case would be to allow downstream users of my API to specify custom values, but provide an enum with standard/common implementations. My interface only requires a String name() , so any enum implements it already. For example, the CopyOption interface in the Java NIO APIs, with the