Typescript has unions, so are enums redundant?

With the recent versions of TypeScript, it is easy to declare iterable union types. Therefore, you should prefer union types to enums. How to declare iterable union types const permissions = [‘read’, ‘write’, ‘execute’] as const; type Permission = typeof permissions[number]; // ‘read’ | ‘write’ | ‘execute’ // you can iterate over permissions for (const … Read more

Why do enum permissions often have 0, 1, 2, 4 values?

Because they are powers of two and I can do this: var permissions = Permissions.Read | Permissions.Write; And perhaps later… if( (permissions & Permissions.Write) == Permissions.Write ) { // we have write access } It is a bit field, where each set bit corresponds to some permission (or whatever the enumerated value logically corresponds to). … Read more

How to add extension methods to Enums

According to this site: Extension methods provide a way to write methods for existing classes in a way other people on your team might actually discover and use. Given that enums are classes like any other it shouldn’t be too surprising that you can extend them, like: enum Duration { Day, Week, Month }; static … Read more

Why shouldn’t Java enum literals be able to have generic type parameters?

This has been discussed as of JEP-301 Enhanced Enums, which was withdrawn, regrettably. The example given in the JEP is, which is precisely what I was looking for: enum Argument<X> { // declares generic enum STRING<String>(String.class), INTEGER<Integer>(Integer.class), … ; Class<X> clazz; Argument(Class<X> clazz) { this.clazz = clazz; } Class<X> getClazz() { return clazz; } } … Read more

Pass enums in angular2 view templates

Create an Enum: enum ACTIVE_OPTIONS { HOME = 0, USERS = 1, PLAYERS = 2 } Create a component, be sure your enum list will have the typeof: export class AppComponent { ACTIVE_OPTIONS = ACTIVE_OPTIONS; active: ACTIVE_OPTIONS; } Create a template: <li [ngClass]=”{ ‘active’: active === ACTIVE_OPTIONS.HOME }”> <a routerLink=”/in”> <i class=”fa fa-fw fa-dashboard”></i> Home … Read more

convert an enum to another type of enum

Given Enum1 value = …, then if you mean by name: Enum2 value2 = (Enum2) Enum.Parse(typeof(Enum2), value.ToString()); If you mean by numeric value, you can usually just cast: Enum2 value2 = (Enum2)value; (with the cast, you might want to use Enum.IsDefined to check for valid values, though)

Java Enum definition

It means that the type argument for enum has to derive from an enum which itself has the same type argument. How can this happen? By making the type argument the new type itself. So if I’ve got an enum called StatusCode, it would be equivalent to: public class StatusCode extends Enum<StatusCode> Now if you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)