Enums
Demonstrates enumerated types; Go simulates them with iota, JS with frozen objects, while Rust enums are the more powerful ADT.
C C23
#include <stdio.h>
#include <string.h>
// C enum: integer constant enumeration; associated values, methods and reverse lookup need hand-written mapping functions
typedef enum {
SUIT_HEARTS,
SUIT_SPADES
} Suit;
const char *suit_value(Suit s) { // equivalent to PHP's backed value (string enum)
switch (s) {
case SUIT_HEARTS: return "Hearts";
case SUIT_SPADES: return "Spades";
}
return "";
}
const char *suit_color(Suit s) { // enum method: emulated with a function
switch (s) {
case SUIT_HEARTS: return "Red";
case SUIT_SPADES: return "Black";
}
return "";
}
const char *suit_name(Suit s) { // case name
switch (s) {
case SUIT_HEARTS: return "Hearts";
case SUIT_SPADES: return "Spades";
}
return "";
}
// equivalent to Suit::from("Spades"): reverse lookup by backed value
Suit suit_from_value(const char *value) {
for (Suit s = SUIT_HEARTS; s <= SUIT_SPADES; s++) {
if (strcmp(suit_value(s), value) == 0) return s;
}
return SUIT_HEARTS; // not found: C has no Option, simplified here
}
int main(void) {
Suit s = SUIT_HEARTS;
printf("%s\n", suit_value(s));
printf("%s\n", suit_color(s));
printf("%s", suit_name(suit_from_value("Spades")));
return 0;
}
Hearts Red Spades
C++ C++26
#include <iostream>
#include <stdexcept>
#include <string>
enum class Suit { Hearts, Spades }; // enum class: strongly-typed enumeration
std::string suitValue(Suit s) {
switch (s) {
case Suit::Hearts: return "Hearts";
case Suit::Spades: return "Spades";
}
throw std::logic_error("Unknown");
}
std::string suitColor(Suit s) {
return s == Suit::Hearts ? "Red" : "Black";
}
std::string suitName(Suit s) {
return s == Suit::Hearts ? "Hearts" : "Spades";
}
Suit suitFromValue(const std::string& v) { // reverse lookup by value
return v == "Hearts" ? Suit::Hearts : Suit::Spades;
}
int main() {
Suit s = Suit::Hearts;
std::cout << suitValue(s) << "\n";
std::cout << suitColor(s) << "\n";
std::cout << suitName(suitFromValue("Spades")) << "\n";
return 0;
}
Hearts Red Spades
C# v11.0.100
var s = Suit.Hearts;
Console.WriteLine(s.Value()); // backed value
Console.WriteLine(s.Color()); // call the extension method
Console.WriteLine(Suit.Spades); // print the case name
enum Suit { Hearts, Spades } // enum members
static class SuitExt {
public static string Value(this Suit s) => s switch { // extension method: approximates an enum method
Suit.Hearts => "Hearts",
Suit.Spades => "Spades",
_ => throw new ArgumentException(),
};
public static string Color(this Suit s) => s switch {
Suit.Hearts => "Red",
_ => "Black",
};
}
Hearts Red Spades
Go v1.27.4
package main
import "fmt"
// Go has no enum type: simulate with iota auto-incrementing constants + a named type
type Suit int
const (
Hearts Suit = iota // 0
Spades // 1
)
func (s Suit) String() string { // attach a string representation to the constants
return [...]string{"Hearts", "Spades"}[s]
}
func main() {
fmt.Println(Hearts.String())
fmt.Println(int(Spades))
}
Hearts 1
Java v27.0.1
public class Main {
enum Suit { // Java enums are full classes: fields, constructors and methods allowed
HEARTS("Hearts"), SPADES("Spades");
private final String label;
Suit(String label) { this.label = label; }
String label() { return label; }
}
public static void main(String[] args) {
Suit s = Suit.HEARTS;
System.out.println(s.label());
System.out.println(Suit.valueOf("SPADES")); // reverse lookup by name
}
}
Hearts SPADES
JavaScript v26.13.1
// JS has no native enums; convention is a frozen object (TS has enum syntax)
const Suit = Object.freeze({
HEARTS: "Hearts",
SPADES: "Spades",
});
console.log(Suit.HEARTS);
console.log(Object.keys(Suit).length);
// Suit.HEARTS = "x" throws TypeError in strict mode
Hearts 2
PHP v8.6.0
enum Suit: string { // PHP 8.1+ native enums (may carry string/int values)
case Hearts = "Hearts";
case Spades = "Spades";
public function color(): string { // enums can define methods
return match($this) {
Suit::Hearts => "Red",
Suit::Spades => "Black",
};
}
}
$s = Suit::Hearts;
echo $s->value, "\n"; // backed value
echo $s->color(), "\n"; // call the enum method
echo Suit::from("Spades")->name; // reverse lookup by value, take the case name
Hearts Red Spades
Python v3.15.2
from enum import Enum
class Suit(Enum): # inherit Enum to define an enumeration
HEARTS = "Hearts"
SPADES = "Spades"
s = Suit.HEARTS
print(s.value) # the member's value
print(Suit("Spades")) # reverse lookup the member by value
Hearts Suit.SPADES
Rust v1.99.1
// Rust enums are algebraic data types (ADT): each variant may carry a different type of data,
// stronger than traditional enums (pairs with match for exhaustive checking; see the instanceof/match entries)
enum Shape {
Circle(f64), // radius
Rectangle(f64, f64), // width and height
}
fn area(s: &Shape) -> f64 {
match s {
Shape::Circle(r) => 3.14 * r * r,
Shape::Rectangle(w, h) => w * h,
}
}
fn main() {
let c = Shape::Circle(2.0);
let r = Shape::Rectangle(3.0, 4.0);
println!("{:.2} {:.2}", area(&c), area(&r));
}
12.56 12.00
TypeScript v26.13.1
// type-stripping runtimes do not support native enum; emulate with an as const object + type (native enum needs the full compiler)
const Suit = {
Hearts: "Hearts",
Spades: "Spades",
} as const;
type SuitName = keyof typeof Suit; // "Hearts" | "Spades"
function color(s: SuitName): string {
return s === "Hearts" ? "Red" : "Black";
}
console.log(Suit.Hearts); // backed value
console.log(color("Hearts")); // emulate an enum method
// reverse lookup by value, take the case name
const name = (Object.keys(Suit) as SuitName[]).find((k) => Suit[k] === "Spades");
console.log(name);
Hearts Red Spades
Language differences
C: an enum is an integer constant; associated values, methods, and reverse lookup are hand-written mappings with no runtime checks.
Go: simulation — no enums; iota constants + a named type + String() (a named type over an integer rather than a plain integer, with no type-safe reverse lookup and not a closed set).
Java: native enum (a full-featured class under the hood, with fields/constructors/methods and valueOf() for reverse lookup).
JavaScript: no native enums; conventionally uses frozen objects.
PHP: native enums — 8.1+ enum (pure and backed enums, methods allowed, reverse lookup via from()/tryFrom()).
Python: native enum.Enum (3.4+, with StrEnum since 3.11).
Rust: enums are ADTs — variants can carry data, making them far more expressive than enums in other languages (equivalent to tagged unions).
TypeScript: native enum requires the full compiler; the type-stripping runner uses an as const object instead.