22,860 questions
1
vote
6
answers
223
views
Why do we need abstract methods in Enums
I've been studying Java for some time now to better understand what goes on inside some of the code parts.
While studying Enum, which I am used to using exclusively for listing various fixed values (...
1
vote
1
answer
104
views
Avoid boxing of generic enum type
Given the base class:
public abstract class EnumType<TEnum, TValue>
where TEnum : struct, IConvertible
where TValue : struct
{
public abstract TEnum GetEnumValue(TValue value);
}
...
Advice
0
votes
0
replies
23
views
Is it possible to generate string fields and const classes from nullable open enums in an OpenAPI spec?
I'm trying to generate a C# client from the OpenRouter OpenAPI spec:
https://github.com/OpenRouterTeam/typescript-sdk/blob/main/.speakeasy/in.openapi.yaml
Among other things there are some enums ...
0
votes
2
answers
154
views
Any Clang diagnostic flag for preventing casting an invalid int value to an enum in C?
Clang's -Wassign-enum diagnostic flag will trigger a warning when using an int whose value is out of scope of an enum's cases:
typedef enum Foo {
FooA,
FooB,
} Foo;
void handleFoo(Foo foo);
...
2407
votes
32
answers
1.7m
views
How to get an enum value from a string value in Java
Say I have an enum which is just
public enum Blah {
A, B, C, D
}
and I would like to find the enum value of a string, for example "A" which would be Blah.A. How would it be possible to ...
735
votes
45
answers
1.1m
views
How to get names of enum entries?
I would like to iterate a TypeScript enum object and get each enumerated symbol name, for example:
enum myEnum { entry1, entry2 }
for (var entry in myEnum) {
// use entry's name here, e.g., "...
1472
votes
31
answers
813k
views
JavaScriptSerializer - JSON serialization of enum as string
I have a class that contains an enum property, and upon serializing the object using JavaScriptSerializer, my json result contains the integer value of the enumeration rather than its string "...
1140
votes
43
answers
1.3m
views
How can I represent an 'Enum' in Python?
I'm mainly a C# developer, but I'm currently working on a project in Python.
How can I represent the equivalent of an Enum in Python?
1468
votes
22
answers
532k
views
Create Generic method constraining T to an Enum
I'm building a function to extend the Enum.Parse concept that
Allows a default value to be parsed in case that an Enum value is not found
Is case insensitive
So I wrote the following:
public static ...
575
votes
34
answers
397k
views
How to convert an enum to a string in modern C++
Contrary to all other similar questions, this question is about using the new C++ features.
2008 c Is there a simple way to convert C++ enum to string?
2008 c Easy way to use variables of enum types ...
963
votes
37
answers
832k
views
String representation of an Enum
I have the following enumeration:
public enum AuthenticationMethod
{
FORMS = 1,
WINDOWSAUTHENTICATION = 2,
SINGLESIGNON = 3
}
The problem however is that I need the word "FORMS" ...
2
votes
2
answers
157
views
Niche optimization: why is `size_of::<Result<bool, bool>>()` 2 instead of 1?
Niche optimization allows Rust to store data in the invalid bit patterns of a type. The only valid values of a bool are 0 and 1 (one bit). And Result only has two variants (one bit). So why does ...
291
votes
33
answers
562k
views
TypeScript enum to object array
I have an enum defined this way:
export enum GoalProgressMeasurements {
Percentage = 1,
Numeric_Target = 2,
Completed_Tasks = 3,
Average_Milestone_Progress = 4,
Not_Measured = 5
}
...
4
votes
3
answers
424
views
Backward and forward compatibility issues with protobufs in Google Pub/Sub
We use protocol buffers both for gRPC server-to-server communication and for publishing messages to Pub/Sub.
Pub/Sub is fairly sensitive to schema changes, not allowing any schema changes that would ...
0
votes
5
answers
3k
views
TypeScript Enum alternative following the use of --erasableSyntaxOnly flag
TypeScript has added the erasableSyntaxOnly flag, that doesn't let you use Enums.
Have looked for several alternative options. Like:
What's the best practice to implement an alternative to ...
824
votes
15
answers
700k
views
What is an idiomatic way of representing enums in Go?
I'm trying to represent a simplified chromosome, which consists of N bases, each of which can only be one of {A, C, T, G}.
I'd like to formalize the constraints with an enum, but I'm wondering what ...
3
votes
1
answer
70
views
Trying to reduce the verboseness of __post_init__ in a python dataclass
I am writing a Python config script that creates an array of input files in a domain-specific language (DSL), so my use case is a bit unusual. In this scenario, we want medium-level users to be able ...
1110
votes
13
answers
472k
views
What is a typedef enum in Objective-C?
I don't think I fundamentally understand what an enum is, and when to use it.
For example:
typedef enum {
kCircle,
kRectangle,
kOblateSpheroid
} ShapeType;
What is really being ...
260
votes
22
answers
317k
views
Enums in Javascript with ES6
I'm rebuilding an old Java project in Javascript, and realized that there's no good way to do enums in JS.
The best I can come up with is:
const Colors = {
RED: Symbol("red"),
BLUE: ...
1
vote
0
answers
49
views
Static typing for an `Enum` that contains compiled regex patterns
I am in a scenario (that could be compared to writing an AST) where I compile multiple regex patterns that I want to reuse later, and for the sake of simplicity and readability I want to store them in ...
395
votes
10
answers
353k
views
Convert string to Enum in Python
What's the correct way to convert a string to a corresponding instance of an Enum subclass? Seems like getattr(YourEnumType, str) does the job, but I'm not sure if it's safe enough.
As an example, ...
227
votes
32
answers
198k
views
Dart How to get the name of an enum as a String
Before enums were available in Dart I wrote some cumbersome and hard to maintain code to simulate enums and now want to simplify it. I need to get the name of the enum as a string such as can be done ...
2
votes
2
answers
98
views
Is there a way to overwrite an enum variable and get a reference to the associated data together?
Here is a simple program that overwrites an enum variable with another variant with associated data, and then needs a reference to that associated data afterward. (A more realistic example would have ...
1
vote
1
answer
89
views
Simplifying getting a specific type from an enum
I have a set of classes linked into an enum:
#[derive(Default)]
pub struct A {
}
#[derive(Default)]
pub struct B {
}
#[derive(Default)]
pub struct C {
}
enum Classes {
A(A),
B(B),
C(C),
...
167
votes
8
answers
274k
views
String-based enum in Python
To encapsulate a list of states I am using enum module:
from enum import Enum
class MyEnum(Enum):
state1='state1'
state2 = 'state2'
state = MyEnum.state1
MyEnum['state1'] == state # here ...