Enums

>enums

Attached: 1565843469699.jpg (224x224, 17K)

Other urls found in this thread:

eel.is/c draft/dcl.enum#8
twitter.com/AnonBabble

create table some_table
{
some_column varchar(50)

, constraint not_enum check (some_column in ('this is an enum value', 'so is this'))

}

seriously, what are they for
who even uses them

.t go user

Good for holding vars that need to be operated on by bitwise ops.

from summerfags to freshman unifags
fucking hell

Just things that people find that makes their lives easier.

enum thing_t {
THING0, THING1
};

enum thing_t a = 45;

And that's a GOOD thing.

>C
Xd

>language has enums instead of algebraic data types
I honestly can't go back after experiencing sum-types

We're seeing more and more languages these days using the "enum" keyword to actually mean sum types. Scala 3 will even let you have GADTs this way.

Enums are great for setting states desu.

>what is a typedef

>a = 45;

enum class in c++11 won't let you do that. You even need to cast that enum back to int if you want to use it as a number. Kinda hate it, but gotta deal with it at work

>rebbit memes

Except go has enums

Associating key/value pairs for making immutable contracts between services.

You can still do
static_cast(45);
There is no protection from accidentally or not casting to an unrepresented enum value. Its UB, but because its technically an int underneath its maybe not UB. The standard conflicts itself IIRC.

>enums are not support by the language
DROPPED

Lightning?????

tagged unions

you can, but generally casts like this are banned in the code guide, enum class is just additional layer of safety so you don't accidentally assign a weird value to it, not completely forbid you to do that because there might be a valid case.

Underrated post

>tfw learned about enums just today from the k&r book and intend to use them every opportunity i can
im going to use it every opportunity i can

Attached: 576889754.gif (500x362, 3.86M)

If you program a stoplight, where do you place the lights (green, yellow, red) variables else? Are you going to make them objects like a faggot or an enum like a chad?

>learned about enums today
You must be 18+ to use this website

what the fuck are you talking about? enums are great you fucking imbecile

don't tell me that you pass around strings or meaningless numbers

it's not UB, enum classes can be used as opaque handles and the rules regarding strict aliasing say this is a-okay
read your fucking standard

You can use them to turn a bare array into a poor man's hash table without the overhead of having to actually hash anything nor the massive amount of memory wasted.
You can also use them for bitwise flags

Yes I see. It's not UB for enum class, but for regular enums perhaps it isn't? It depends on how you interpret what a fixed underlying type means in eel.is/c draft/dcl.enum#8
Dont all enums have a fixed underlying type? It's either implicitly int or with enum classes it can be specified.

according to cppreference, the implicit underlying type for unscoped enums is implementation defined, with scoped it's always an int

However, i just tested it, and unscoped enums can also be explicitly specified, so I guess that as long as it's explicitly specified it's okay?

Sum types for the chads

enum classes are useful as hell

In java enums work well for things that have common properties and need to be able to be listed programatically (e.g. for a GUI).

You use them in Python to link positional and value indexing.

the fuck would you use instead?

I encountered only one person who expressed disdain for enums: a PHP developer.

>PHP
>developer

strings

def enum(**enums):
return type('Enum', (), enums)
BAR = enum(
INIT = 0,
IDLE = 1,
END = 2,
)

BEHOLD THE POWER OF PYTHON

Attached: 1559866061657.jpg (720x690, 72K)

You are going to do a string compare when you could do a byte compare? Also, you are going to introduce typo bugs that can't be caught by a compiler?

Attached: doofus.jpg (302x167, 6K)

dynamic keys

like in a hash map? you are now using hash maps to implement what was done with an int compare? again, this doesn't seem to solve the type bugs I mentioned

but it's better because you can create brand new key-value pairs at runtime

enums and enum classes can both have explicit underlying types.

if there isn't an explicit type, then:

-in case of a scoped enum (enum class), the underlying type is int

-in case of an unscoped enum (plain enum), the underlying type is an integral type that can represent all the enumerator values defined in the enumeration.
It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int.

basically, you can safely cast any enum/enum class to an integer type, as long as that integer can represent that enumerator, and vice-versa. Using auto helps to prevent implicit conversions here. For example:
#include
#include

template
constexpr auto to_underlying_type(E e) -> std::underlying_type_t {
return static_cast(e);
}

enum class Letter {
A,
B,
C,
};

char letter_to_ascii(Letter l) {
static auto constexpr letter_ascii_map = std::array{
'A', 'B', 'C',
};
return letter_ascii_map[to_underlying_type(l)];
}

C++11 has enum class, thx god!

I use them for a finite state machine so that the states have names that are easy to read. What would you suggest I use instead?

i pun void pointers and other opaque handle types that are just typedefs of void pointers to enum classes with an underlying type of size_t to allow for type safety

pub enum Value {
Null,
Bool(bool),
Number(Number),
String(String),
Array(Vec),
Object(Map),
}

Attached: 1564846687099_0.png (1799x825, 263K)

That's retarded. You can just use forward declared struct pointers to get opaque handles, and you should use (u)intptr_t instead of size_t to convert pointers to integers.

one of the reasons enum classes are a thing is for this exact use
though you are probably right, i should switch to uintptr_t
why do you think can do this

enum class handle : size_t {
INVALID_VALUE = -1,
};

auto handle_v = handle{45};

Just use an array of strings instead.

Enums were invented because we can no longer use booleans to determine someone's gender.

Enums help us avoid hardcoding everything which will lead to less problems.

Sepples stealing ideas from Java? How the turn tables.

>Java invented strongly typed enums

how so?