Skip to content

enum — examples

← all topics · 8 examples · page 1 of 1 · raw source ↓

Runs `go` → "E"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::CrossNamespace_EnumMatched   topic: enum   status: verified
// verified behavior: Test.go(...) == "E"

namespace Model

enum Role {
    admin
    editor
    viewer
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Enum_ExplicitValues_CorrectConstants   topic: enum   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

enum Priority {
    low = 1
    medium = 5
    high = 10
}

func test() -> int {
    return 0
}

Runs `test` → 2

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Enum_Member_Access_And_Comparison_Pin   topic: enum   status: verified
// verified behavior: Test.test(...) == 2

namespace Test

enum Suit { hearts, diamonds, clubs, spades }

func is_red(s: Suit) -> bool {
    if s == .hearts { return true }
    if s == .diamonds { return true }
    return false
}

func test() -> int {
    var red = 0
    if is_red(.hearts)   { red = red + 1 }
    if is_red(.diamonds) { red = red + 1 }
    if is_red(.clubs)    { red = red + 1 }
    if is_red(.spades)   { red = red + 1 }
    return red
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Enum_MixedAutoAndExplicit   topic: enum   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

enum Level {
    a
    b = 10
    c
}

func test() -> int {
    return 0
}

Runs `test` → 210

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Enum_Cases_Have_Sequential_Values   topic: enum   status: verified
// verified behavior: Test.test(...) == 210

namespace Test

enum Code { a, b, c }

func ord(c: Code) -> int {
    if c == .a { return 0 }
    if c == .b { return 1 }
    if c == .c { return 2 }
    return -1
}

func test() -> int {
    return ord(.a) + ord(.b) * 10 + ord(.c) * 100
}

Rejected at compile time: ES2145

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: IndexDiagnosticTests.cs::IndexingEnum_Errors   topic: enum   status: verified
// verified behavior: reports diagnostic ES2145

namespace Test

enum E { a  b  c }

func f(e: E) -> int { return e[0] }

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_Enum_Declaration   topic: enum   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Dir

pub enum Direction {
    north
    south
}

Runs `test` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Enum_Equality   topic: enum   status: unverified
// verified behavior: Test.test(...) == true

namespace Test

enum Code { a, b, c }

func test() -> bool {
    let x = Code.a
    let y = Code.a
    return x == y
}