Skip to content

choice — examples

← all topics · 159 examples · page 4 of 4 · raw source ↓

Runs `go` → 100

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_CaseView.cs::View_SinglePayload_ArithmeticOnProjection   topic: choice   status: unverified
// verified behavior: Test.go(...) == 100

func go() -> int {
  let p = Pair2.single(10)
  match (p: Pair2) { .single(s) { return s.x * s.x } .couple(c) { return 0 } }
  return -1
}

Runs `go` → 9

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_CaseView.cs::View_SinglePayload_BareUse   topic: choice   status: unverified
// verified behavior: Test.go(...) == 9

func go() -> int {
  let p = Pair2.single(9)
  match (p: Pair2) { .single(s) { return s } .couple(c) { return 0 } }
  return -1
}

Runs `go` → 9

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_CaseView.cs::View_SinglePayload_FieldAccess   topic: choice   status: unverified
// verified behavior: Test.go(...) == 9

func go() -> int {
  let p = Pair2.single(9)
  match (p: Pair2) { .single(s) { return s.x } .couple(c) { return 0 } }
  return -1
}

Runs `go` → "hello"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_CaseView.cs::View_StringPayloadProjection   topic: choice   status: unverified
// verified behavior: Test.go(...) == "hello"

func go() -> string {
  let t = Token.word("hello")
  match (t: Token) { .word(w) { return w.text } .number(n) { return "n" } }
  return "?"
}

Runs `go` → 6

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_CaseView.cs::View_ThreePayloads   topic: choice   status: unverified
// verified behavior: Test.go(...) == 6

func go() -> int {
  let t = Triple.only(1, 2, 3)
  match (t: Triple) { .only(v) { return v.a + v.b + v.c } }
  return -1
}

Compiles

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

func go() -> string {
  let p = Pair2.couple(5, 6)
  match (p: Pair2) { .single(s) { return "{s.x}" } .couple(c) { return "x={c.a},y={c.b}" } }
  return "?"
}

Runs `go` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::GenericEquality_ReturnedFromMatch   topic: choice   status: unverified
// verified behavior: Test.go(...) == true

func go() -> bool {
  let a = Pair<int, int> { first: 2, second: 3 }
  let b = Pair<int, int> { first: 2, second: 3 }
  let eq = a.Equals(b)
  return match eq { true { true } false { false } }
}

Runs `go` → "hello"

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


func go() -> string {
    let r = parse("\"hello\"")
    if r.IsError { return "ERR" }
    match r.Value {
        .jstr(node) { return node.s }
        default { return "?" }
    }
    return "?"
}

Compiles

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

namespace Auth

pub data LoginRequest {
    email: string
    password: string
}

pub choice AuthError {
    invalidCredentials
    accountLocked(untilUtc: DateTimeOffset)
}

pub func makeRequest(email: string, password: string) -> LoginRequest {
    let req = LoginRequest {
        email: email
        password: password
    }
    return req
}

pub func login(req: LoginRequest) -> Result<LoginRequest, AuthError> {
    if req.password == "secret" {
        return ok(req)
    }

    return error(AuthError.invalidCredentials())
}