Toolvito

Works on this device

Enum Generator

Type a list of names and choose a language. The tool writes the enum in the usual style of that language, with counted values, your own numbers, flags or text values. Names that the language does not allow are refused, with the reason. Copy the code or download it as a file.

Enter your values

Your values

Words such as http status are joined into HttpStatus.

Words such as dark green become DarkGreen or DARK_GREEN. Write name = value to give one member its own value. Names with commas between them on one line also work.

More options

Words put in front of each member, such as the enum name in C or Go (COLOR_RED, ColorRed). It follows the case style.

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Fill in the values, then choose Make the enum.

How to use the enum generator

  1. Choose the Language, and type the Enum name, such as Color or http status.
  2. Type the names, one on each line, such as Red, Dark green and Light blue. Names with commas between them on one line work too.
  3. Choose the Case style of the members. The usual style of the language is the starting choice.
  4. Choose the Values: counted by the language, whole numbers from a first number, flags (1, 2, 4, 8 and so on) or text values. To give one member its own value, write name = value, such as Urgent = 10.
  5. The code is written while you type and is shown with the colours of its language. Copy it, or download it as a file: both give the plain code, without colours. Under More options you can add a prefix and choose the indent.

How a name becomes a member

  • Accents are taken off, so café becomes cafe. A letter that is not one of A to Z after that, such as ß, is refused.
  • Every other character, such as a space, -, _ or /, ends a word and is dropped. A word is also split where a small letter or a digit is followed by a capital letter (darkGreen is dark and Green), and before the last capital of a run of capitals that is followed by a small letter (HTTPStatus is HTTP and Status).
  • The words of the prefix go first. Then the words are written in the case style: DarkGreen, DARK_GREEN, darkGreen or dark_green. The enum name is always written in PascalCase. With As typed, every name and the enum name are used exactly as you typed them.
  • Each name is then checked: it must have a letter, must not start with a digit, may hold only A to Z, digits and _, and must not be a reserved word of the language. Two names that become the same member are refused too.

The rules of each language

Each language has its own usual style and its own kinds of values. Counted values are the values the language gives by itself when none are written.

Languages, usual styles and values
Language Usual style of members Values possible First member when the language counts
TypeScript PascalCase (DarkGreen) Let the language count; Whole numbers, written out; Flags; Text values 0 (counted by TypeScript)
JavaScript UPPER_SNAKE_CASE (DARK_GREEN) Let the language count; Whole numbers, written out; Flags; Text values 0 (written out)
Python UPPER_SNAKE_CASE (DARK_GREEN) Let the language count; Whole numbers, written out; Flags; Text values 1 (counted by auto())
Java UPPER_SNAKE_CASE (DARK_GREEN) Let the language count; Whole numbers, written out; Flags; Text values ordinal 0
C# PascalCase (DarkGreen) Let the language count; Whole numbers, written out; Flags 0 (counted by C#)
C UPPER_SNAKE_CASE (DARK_GREEN) Let the language count; Whole numbers, written out; Flags 0 (counted by C)
C++ PascalCase (DarkGreen) Let the language count; Whole numbers, written out; Flags 0 (counted by C++)
Go PascalCase (DarkGreen) Let the language count; Whole numbers, written out; Flags; Text values 0 (counted by iota)
Kotlin UPPER_SNAKE_CASE (DARK_GREEN) Let the language count; Whole numbers, written out; Flags; Text values ordinal 0
PHP PascalCase (DarkGreen) Let the language count; Whole numbers, written out; Flags; Text values none (a pure enum)

What the code looks like

  • TypeScript: TypeScript: the enum name and the members are usually written in PascalCase. A member name may be any name, also a reserved word such as default; the enum name may not be a reserved word or a built-in type name such as string. Without values TypeScript numbers the members 0, 1, 2 and so on. A member with its own number makes the next ones count on from it. A text enum has a value for every member.
  • JavaScript: JavaScript has no enum type. The usual form is a frozen object: Object.freeze stops the members from being changed. Members are usually written in UPPER_SNAKE_CASE, and any name, also a reserved word, may be a key. JavaScript does not count by itself, so the numbers 0, 1, 2 and so on are written out.
  • Python: Python: the enum is a class of the enum module. Members are usually written in UPPER_SNAKE_CASE, as PEP 8 writes constants. A keyword such as class or None cannot be a name. auto() numbers the members of an Enum from 1. Members of IntEnum and IntFlag can be compared with whole numbers, and IntFlag members can be combined with |.
  • Java: Java: members are usually written in UPPER_SNAKE_CASE and the enum name in PascalCase. A keyword, true, false, null or _ cannot be a name, and var, yield, record, sealed and permits cannot be the enum name. Without values each member has its place, ordinal(), from 0. A member with a value needs a field and a constructor, written after the members and a semicolon. A public enum is saved in a file with its own name.
  • C#: C#: the enum name and the members are usually written in PascalCase. A keyword such as class cannot be a name. Without values the members are numbered 0, 1, 2 and so on; a member with its own number makes the next ones count on from it. C# enums hold whole numbers only. [Flags] makes .NET show a combined value by its member names, such as Read, Write.
  • C: C: members are usually written in UPPER_SNAKE_CASE. They share one scope with every other name of the file, so a prefix such as the enum name (COLOR_RED) keeps them apart. A keyword (C17 and C23) cannot be a name, and this tool does not write names that start with _. Without values the members are numbered 0, 1, 2 and so on; a member with its own number makes the next ones count on from it. C enums hold whole numbers only.
  • C++: C++: enum class keeps the members inside the enum (Color::Red), so no prefix is needed; they are often written in PascalCase. A keyword (C++20) cannot be a name, and this tool does not write names that start with _ or hold __. Without values the members are numbered 0, 1, 2 and so on. An enum class holds whole numbers only, and | and & on it need operators that you write.
  • Go: Go has no enum type. The usual form is a named type and a block of constants. A name that starts with a capital letter can be used from other packages. The constants are names of the package, so they usually start with the type name (ColorRed): type the enum name as the prefix. Without values iota counts 0, 1, 2 and so on in the block. Go code is indented with tabs, as gofmt writes it.
  • Kotlin: Kotlin: the members of an enum class are usually written in UPPER_SNAKE_CASE. A hard keyword such as when or object cannot be a name. Without values each member has its place, ordinal, from 0. A value is a property in the constructor of the enum class.
  • PHP: PHP 8.1 and later: cases are usually written in PascalCase. A case cannot be called class, and the enum name cannot be a reserved word such as list or match, in any mix of capital and small letters. An enum without values is a pure enum. With int or string values it is a backed enum: every case has a value, and two cases may not have the same value. PHP enums cannot be combined with |; combine the ->value numbers for flags.

Worked example

With the starting values (TypeScript, enum name Color, the names Red, Dark green and Light blue, values counted by the language) the tool writes export enum Color { with the members Red,, DarkGreen, and LightBlue,, each on its own line and indented by 4 spaces, and a closing }. TypeScript numbers them 0, 1 and 2.

For C# with the enum name Permission, the names Read, Write, Execute and Delete, and the values Flags, the tool writes using System;, then [Flags] above public enum Permission, and the members None = 0, Read = 1, Write = 2, Execute = 4 and Delete = 8.

For Python with the names class and Pass in snake_case, the tool refuses the first name: class is a reserved word of Python.

Limits you should know

  • The code is written as text. It is not run or compiled here, so test it in your project.
  • Names use the letters A to Z, digits and _ only, although some languages allow other letters too.
  • At most 500 names; at most 31 flags, so that the largest value, 1,073,741,824, fits a 32-bit int. Whole numbers must lie between -2,147,483,648 and 2,147,483,647.
  • C#, C and C++ enums hold whole numbers only, so text values are not offered there.
  • To branch on the members of an enum, use the Switch Case Generator. To see other code in colour, use the Syntax Highlighter.

Related tools