Skip to content

union — examples

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

Compiles

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

func go() -> string =
    runStat()
func runStat() -> string {
    let s: Sym = TypeSym("a", 1)
    return match s {
        (t: TypeSym) => "type {t.name}"
    }
}

Runs `go` → 2

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

func go() -> int {
  let p = Pair2.single(2)
  match (p: Pair2) { .single(s) { return s.x } .couple(a, b) { return a + b } }
  return -1
}

Runs `go` → "num:7"

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

func desc(tok: Token) -> string {
  match (tok: Token) { .word(w) { return "w:{w.text}" } .number(n) { return "num:{n.value}" } }
  return "?"
}
func go() -> string {
  let lx = Lex.one(Token.number(7))
  match (lx: Lex) { .one(o) { return desc(o.t) } .pair(p) { return "pair" } }
  return "?"
}

Runs `go` → "hi/9"

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

func desc(tok: Token) -> string {
  match (tok: Token) { .word(w) { return w.text } .number(n) { let v = n.value
    return "{v}" } }
  return "?"
}
func go() -> string {
  let lx = Lex.pair(Token.word("hi"), Token.number(9))
  match (lx: Lex) { .one(o) { return "one" } .pair(p) { return "{desc(p.a)}/{desc(p.b)}" } }
  return "?"
}

Runs `go` → 3

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

func go() -> int {
  let outer = Pair2.couple(1, 2)
  match (outer: Pair2) {
    .single(c) { return c.x }
    .couple(c) {
      let inner = Pair2.single(3)
      match (inner: Pair2) { .single(c) { return c.x } .couple(c) { return c.a } }
      return -2
    }
  }
  return -1
}

Runs `go` → 7

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

func go() -> int {
  let t = Token.number(7)
  match (t: Token) { .word(w) { return 0 } .number(n) { return n } }
  return -1
}

Runs `go` → 6

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

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

Runs `go` → 30

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

func go() -> int {
  let p = Pair2.couple(10, 20)
  match (p: Pair2) { .single(x) { return x } .couple(a, b) { return a + b } }
  return -1
}

Runs `go` → 14

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

func fetch() -> Result<Reply, string> = ok(Reply.accepted(7))
func go() -> int {
  let r = fetch()?
  match (r: Reply) { .accepted(a) { return a.id * 2 } .rejected(x) { return 0 } }
  return -1
}

Runs `go` → "v=42"

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

func go() -> string {
  let p = Pair2.single(42)
  match (p: Pair2) { .single(s) { return "v={s.x}" } .couple(c) { return "?" } }
  return "?"
}

Runs `go` → 11

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

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

Runs `go` → 30

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

func go() -> int {
  let p = Pair2.couple(10, 20)
  match (p: Pair2) { .single(x) { return x } .couple(c) { return c.a + c.b } }
  return -1
}

Runs `go` → 10

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

func go() -> int {
  let p = Pair2.couple(10, 20)
  match (p: Pair2) { .single(x) { return x } .couple(c) { return c.a } }
  return -1
}

Runs `go` → 20

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

func go() -> int {
  let p = Pair2.couple(10, 20)
  match (p: Pair2) { .single(x) { return x } .couple(c) { return c.b } }
  return -1
}

Runs `go` → 7

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

func go() -> int {
  let p = Pair2.couple(3, 4)
  match (p: Pair2) { .single(x) { return x } .couple(c) { let b = c.b
    let a = c.a
    return a + b } }
  return -1
}

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: union   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: union   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: union   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: union   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: union   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: union   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: union   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: union   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 "?"
}

Runs `go` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::H05_Match_ErrPayload   topic: union   status: unverified
// verified behavior: Test.go(...) == 3

func go() -> int {
    match parse(-1) { .ok(v) { return v } .err(e) { return e.Length } }
    return -99
}

Compiles

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

func (s: Sym) describe() -> string =
    match s {
        (t: TypeSym)   => "type {t.name}"
        (m: MethodSym) => "func {m.name}"
    }
func go() -> string {
    let s: Sym = TypeSym("a", 1)
    return s.describe()
}

Compiles

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

func go() -> int {
    let s: Sym = TypeSym("a", 5)
    return match s {
        (t: TypeSym)   => t.arity
        (m: MethodSym) => 0
        default        => -1
    }
}

Compiles

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

func go() -> string {
    let s: Sym = TypeSym("foo", 2)
    return match s {
        (t: TypeSym)   => "type {t.name}/{t.arity}"
        (m: MethodSym) => "func {m.name}"
        default        => s.name
    }
}

Compiles

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

func go() -> string {
    let s: Sym = MethodSym("bar", true)
    return match s {
        (t: TypeSym)                 => "type {t.name}"
        (m: MethodSym) if m.isStatic => "static {m.name}"
        (m: MethodSym)               => "func {m.name}"
        default                      => s.name
    }
}

Compiles

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

namespace Test
} ) > ;; func ( < data %% choice -> *? &( match {{{