Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if value is enum?

Tags:

dart

I would like to check if a value in dart is an Enum, and if so, serialize it automatically.

My sticking point is how to test if a value is an enum.

This is how I am testing for other types

if (T == int)
  val = prefs.getInt(this._key);
else if (T == double)
  val = prefs.getDouble(this._key);
else if (T == bool)
  val = prefs.getBool(this._key);
else if (T == String)
  val = prefs.getString(this._key);
else if ([""] is T)
  val = prefs.getStringList(this._key);

but I cannot seem to compare T to an enum

Since all enums have the same methods I was hoping to sense them and handle them accordingly

like image 673
Luke Pighetti Avatar asked Dec 25 '18 16:12

Luke Pighetti


People also ask

How do you check if a value is in an enum Python?

To check if a value exists in an enum in Python: Use a list comprehension to get a list of all of the enum's values. Use the in operator to check if the value is present in the list. The in operator will return True if the value is in the list.

How do I check if a string is enum?

1).The EnumUtils class has a method called isValidEnum whichChecks if the specified name is a valid enum for the class. It returns a boolean true if the String is contained in the Enum class, and a boolean false otherwise.

Is enum value or reference?

Enum is a reference type, but any specific enum type is a value type. In the same way, System. ValueType is a reference type, but all types inheriting from it (other than System. Enum ) are value types.

How do you equate an enum in Java?

equals method uses == operator internally to check if two enum are equal. This means, You can compare Enum using both == and equals method.

How to check if a string is valid for an enum?

For this, we can create an annotation that checks if the String is valid for a specific enum. This annotation can be added to a String field and we can pass any enum class. Let's define the ValueOfEnumValidator to check whether the String (or any CharSequence) is contained in the enum:

How to check whether a value is in an enumeration in Java?

This article shares with you how to check whether a given value is in an enumeration in java. When we define the enum class, we provide the corresponding check method. This method is suitable for us to use our own defined enum class. For example, the following enum class provides the isExist method to check whether the name exists in the enum.

What are the values of enum values in C++?

To make things worse, they are not continuous (their values goes 0, 75,76,80,85,90,95,100, etc.) Show activity on this post. enum value is valid in C++ if it falls in range [A, B], which is defined by the standard rule below. So in case of enum X { A = 1, B = 3 }, the value of 2 is considered a valid enum value.

Is there a macro trick to check enum values?

There are no macro tricks either. idea. or structure. That way you could control things such as quantity and incrementing values. Nov 14 '05 # 2 I'd like to check if a value is defined in an enum. /* a few hundred more values... */ compile-time, but I'd like to check it at run-time. changes, I'd have to update each validate switch.


1 Answers

As of late 2021, all enums extend Enum

You can now finally use is Enum directly:

enum MyEnum {one, two, three}

var x = "a";
var y = MyEnum.one;

print(x is Enum); // false
print(y is Enum); // true

Which also means you can now create extensions for enums:

extension EnumExtension on Enum { ... }
like image 183
MarcG Avatar answered Oct 05 '22 23:10

MarcG