Questions tagged [enums]
A data type consisting of a set of named values called elements, members or enumerators of the type.
21,116
questions
0
votes
1
answer
13
views
How to access Enum values in Typescript
I have an interface:
export interface Person {
firstName?: string;
lastName?: string;
status?: Status;
}
export enum Status {
Single,
Married,
Divorced
}
if I create a Person
Person john ...
0
votes
1
answer
25
views
How can I use an enum with a switch case in Java? [duplicate]
Previously, I was saving the keys inside SharedPreferencesManager class using this way
public static final int KEY_COUNTRY_ID = 5,
KEY_COUNTRY_NAME = 10,
KEY_CURRENCY_ID = 15,
...
-2
votes
1
answer
55
views
Begginer at OOP - JAVA
I found out the OOP in JAVA just 2 days ago, and now I have a problem that I can not reslove alone.
I have 4 classes for a company, in the 1st class we have the characteristics of an employee(name, ...
2
votes
1
answer
30
views
How to handle future enum values in Dart 2.17+
My app is connected to a custom made peripheral and draws some values from it as ints which I want to convert to enums to streamline the app code.
As the hardware evolves overtime, it is of known fact ...
-3
votes
2
answers
54
views
Sort enum property efficient way [closed]
I have a class with a property that is an enum:
public class Test
{
public HydrEnum Expect { get; set; }
}
The enum declaration looks like this:
public enum HydrEnum
{
Active = 1,
...
3
votes
1
answer
77
views
Are inner enums or class efficient?
Quite often I see people using inner enums, for example:
public class Person {
enum Gender {
MALE,
FEMALE,
OTHER
}
...
}
I am unsure how it works internally, so I am wondering ...
0
votes
2
answers
33
views
Java Spring custom Enum to String conversion in JSON Serialization
I'm trying to convert an enum value into a custom string as part of a JSON response in a Java Spring application. I've attempted to override the enum's toString method and create a Spring converter ...
0
votes
2
answers
29
views
Rewriting data inside a list by enum values
Let's say i have an enum that looks like this:
class MyEnum(str, Enum):
A = 'atk'
C = 'ccc'
B = 'break'
And a list looking like so:
list = ['A', 123, 'B', 44, 2, 'C']
How do i make it ...
0
votes
2
answers
52
views
Enum used partly in each class [closed]
I have some objects that can move on the screen. Each object / group of objects can move a bit differenty.
First can go UP, DOWN, LEFT, RIGHT (kind of floating on the screen (UP & RIGHT will go ...
2
votes
1
answer
26
views
Typing dataclass that can only take enum values
I have a dataclass that can take values that are part of an enum.
class MyEnum(Enum):
A = "valueA"
B = "valueB"
@dataclass
class MyDataclass:
value: MyEnum
When ...
0
votes
1
answer
21
views
SwiftUI use a Binding to some enums
When you press the empty square, you change it to different images. It's for a simple game I created.
Now what I want is to bind these values so that when I go to a different view and come back these ...
0
votes
0
answers
23
views
conditional enum assignment in Typescript interface
I have two enums something like below.
enum Dog {
NAME = 'tommy',
BREED = 'husky'
}
enum Animal {
TYPE = 'type',
DOMESTIC = 'domestic'
}
interface Species {
dog?: string;
pet: dog ? `${Dog}` : `${...
8
votes
1
answer
77
views
Iterating over an enum class: it works, but is it conforming the standard?
In the c++ code below+output, the following is used to iterate over the values of an enum class, say MyEnumClass: (the enum class has contiguous values)
for (const auto& e : MyEnumClass() )
{
// ...
2
votes
2
answers
34
views
Enums in Vue and javascript
I want to make enums work in in vue file.
I first define my enums in a js file
const Status= Object.freeze({
Normal: "normal",
Loading: "loading",
Error: "error&...
0
votes
1
answer
35
views
Is there a way to define a PostgreSQL column as an Enum?
In a PostgreSQL DB, imagine you want to add a column called vehicle_type and you only want one of the following 3 entries to ever possibly appear: car, truck, or motorcycle. I thought there would be ...
1
vote
1
answer
37
views
Unmarshal to Enum type using a custom Scan func
I'm trying to unmarshall a Gender type that is an Enumeration that is just int64 under the hood (I don't want to use any string representation of this type).
Problem is that the Gender value is not ...
0
votes
0
answers
31
views
Can you increment/decrement over a TypeScript Enum to set the condition for a switch statement?
TL;DR: Can you use a TypeScript enum to set a switch statement condition, using onClick functions to move up and down the enum, and keep the result constrained within that enum?
On a project, I needed ...
-4
votes
1
answer
76
views
Java - referencing enums in enum [closed]
I'm trying to create enum which will contain references to different enums.
Something like this:
MainEnum class
public enum MainEnum{
value1(Enum1.class),
value2(Enum2.class);
private ... ...
0
votes
0
answers
8
views
Importing Enum from another alpackage
I have 2 extensions for Business Central, one base and one where I am doing the customizations. I have a Enum in the base extension and the base app is currently in the folder of the extension I am ...
0
votes
1
answer
46
views
avoid implicit enum raw value conversion
I am attempting to avoid the implicit conversion of an enum into a string in a case like this:
enum Animal {
case cat
}
func getTag(forAnimal animal: Animal) -> String {
// fails with &...
2
votes
2
answers
33
views
attributes of attributes of Enums?
I'm not sure, if something like this is possible, but I would like to save more attributes to enum attributes (like value).
Example:
class Unit(Enum):
mm = "millimeter"
Unit.mm.value ...
1
vote
0
answers
44
views
Can I access to Springs components from enum?
I have made this class which creates bundle from database records.
The records are got from stored procedures and names of this procedures are taken from ServiceConfig instance which is Spring @...
0
votes
1
answer
30
views
Enum with another enum as keys
Im working on a project where adapters never raise Exceptions. Instead, it returns a Result object like this:
Dto = TypeVar("Dto", bound=BaseModel)
class BaseError(BaseModel):
...
0
votes
1
answer
28
views
PHP8.1 - Should enums be in their own file?
I want to start making use of enumerations in PHP now that they are natively supported, but I'm having trouble figuring out how to organize and implement them. In my mind, they're more of a well-...
0
votes
0
answers
9
views
Enum of date does not work with timedelta
I have a problem when I try to make an Enum of date (with python 3.10.4).
I cannot use timedelta with a member of the Enum:
from datetime import date, timedelta
from enum import Enum
class DateEnum(...
1
vote
2
answers
89
views
Is there a stratified enum, or base class for enum in C++?
I encountered a situation where I need to write many enums, but wish to separate, or stratify all the enums into groups of enums of smaller length, as the following shows:
enum class health {HP,Shield}...
0
votes
1
answer
42
views
Convert a string to a any data type in kotlin
I am trying to create a generic function to convert any string to any data type.
For example
convert<Int>("23") => 23
convert<LocalDateTime>("2022-06-29T18:02:00") ...
1
vote
1
answer
38
views
How to map Enum attribute into an Enum of another type with Mapstruct - Java
I have two similar enums used respectively in Domain and Entity class.
Here is an Example :
Classes
public class Entity {
String name;
State state;
}
public class Domain {
String name;
...
0
votes
1
answer
29
views
Swift Variables and Enums in ForEach Loop for Bindings
I have a ForEach loop in which I am displaying X amount of fields over and over again depending on user entry (i.e. they enter 5, it displays 5 iterations of these fields).
I need to be able to set ...
1
vote
0
answers
28
views
Mapping JSON object into Typed object with enum property
I am wanting to convert a JSON "string" that I pull from an api into my Typescript model, if i change one of the props from enum to string it works quite easily, however I cannot seem to map ...
-2
votes
0
answers
34
views
How can I use my custom type in a ForEach loop in SwiftUI?
My Item model was getting a little too large, so I split it into InventoryItem and SoldItem. In order to filter through the results for a search bar, I did the following:
var filteredItems: ItemType {
...
-1
votes
1
answer
38
views
Java generic function fail to compile: need interface type
I'm trying a sample code snippet from "Effective Java, item 34". I'm using jdk1.8 on win10.
package mygroup;
import java.util.*;
public class ExtendsEnum {
private static enum Operation ...
-1
votes
1
answer
26
views
Return the built Enum to JSON
I have an object and an enum for it. When I give away an object, I want my enum inside the object to be displayed as an object with the name and value attributes without using DTO, or to be partially ...
-1
votes
0
answers
19
views
How can I hide an enum option from fastapi?
from enum import Enum
class MyEnum(str, Enum):
PRIVATE_OPTION = 'pr'
PUBLIC_1 = 'pb1'
PUBLIC_2 = 'pb2'
I would like to hide 'PRIVATE_OPTION' from fastapi Swagger, but I don't know ...
0
votes
0
answers
15
views
how to define constants or enum in java when a value has a value to store [duplicate]
I am struggling to have a proper implementation in java when it is required to have a set of constants values in String and those strings have the values again in integer/String.
for example,
small-...
0
votes
1
answer
26
views
How to create a switch statement from enum type so it automatically fills case for each enum member?
This feature exists in Visual Studio Professional, but no idea how to do it in VS Code.
For example I have Colors enum like this:
enum Colors {
Red,
Blue,
So when I write a switch statement ...
0
votes
1
answer
33
views
enum class nested in a class compiles on Linux, but not MacOS?
I wrote a basic program to show the issue I'm dealing with.
#include <iostream>
using namespace std;
class SomeClass {
private:
public:
enum class SomeEnum : int {a, b, c};
...
0
votes
1
answer
33
views
Singletons with Dart enums?
One of the recommended ways of creating singletons in Java is to use an enum.
There are other ways of creating singletons in Dart, but with the new enhanced enums I got to wondering if this could be ...
4
votes
2
answers
43
views
How to throw a meaningful error message from an invalid enum request param in spring boot?
I am accepting an enum parameter to my API but when I use an invalid param via Postman I get blank 400 Bad Request response. I want a meaningful error message like : "invalid Type please insert .....
0
votes
1
answer
24
views
How to set the type of a generic enum in Dart
Given the enum below:
enum Animal<T> {
cow,
goat;
}
How do I set the type?
If I do this:
void main() {
final animal = Animal.cow;
}
Dart infers the type of animal to be Animal<dynamic&...
0
votes
1
answer
31
views
Generic FluentValidation Validator with a generic enum
I have been experimenting with generics and tried writing a generic validator using a generic enum.
The setup is as following:
I have an interface:
public interface IHasProperties<TEnum> where ...
1
vote
2
answers
69
views
How to access nested enums without full match syntax
I am using the Serde crate to deserialise a JSON file, which has a nested structure like this:
struct Nested {
a: Vec<Foo>,
b: u8,
}
struct Foo {
c: Bar,
d: Vec<f32>,
}
...
2
votes
1
answer
86
views
Why are we not allowed to override hashcode in enum in java
What I only know for now is that there is no implementation of hashCode() of Enum in java. what it returns is just super.hashCode() while other immutable class such as String all has its own ...
1
vote
1
answer
23
views
Is putting variables from .env/.ini/ in Enum good? [closed]
According to PEP 435 Enum class is
An enumeration is a set of symbolic names bound to unique, constant values.
Within an enumeration, the values can be compared by identity, and the enumeration ...
-1
votes
0
answers
13
views
Can I create a struct where the keys are values of an enum? [duplicate]
I want to create a struct where the keys are values in an enum and values are another struct. Does anyone have any suggestions, or should I just be using a map instead?
type MyKeys string
const (
...
2
votes
1
answer
29
views
Kotlin - categorized subenums
I am trying to implement a Role class/interface/enum that I can use throughout my program. I want the roles to be somewhat categorized - I want some roles to be of type A, some roles to be of type B, ...
1
vote
3
answers
57
views
How to evaluate two enum members with same value as not equal in C#?
I have this enum instance
public enum MyEnum {A1 = 0, A2 = 0}
where I want the members A1 A2 to yield the same value when evaluated but should not be considered equal when compared.
Hence in the ...
1
vote
1
answer
35
views
Accessing global enums in vue
How can I consume global enums in vue, or another way to declare them?
I have the following setup:
In my types/auth.d.ts:
export {};
declare global {
enum MyEnum {
some = "some",
...
1
vote
1
answer
32
views
Create enum of generic variants of a type
I have a struct with a constant generic parameter VAR. This parameter is used in methods, improving the runtime speed, due to large amount of comparisons can be done compile time.
It's rather ...
0
votes
0
answers
31
views
Enum not observed in SwiftUI
I have an enum that represent my response status like this:
enum BaseModelStatus: Equatable {
case success
case fail
}
then according to my response, i will set a value to my enum like this:
@...