result — examples
← all topics · 41 examples · page 1 of 1 · raw source ↓
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Result_OkAndError_RoundTrips topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func getOk() -> Result<int, string> {
return ok(42)
}
func getErr() -> Result<int, string> {
return error("bad")
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::TryUnwrap_PropagatesError topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func inner() -> Result<int, string> {
return error("fail")
}
func outer() -> Result<int, string> {
let x = inner()?
return ok(x + 1)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::TryUnwrap_UnwrapsOk topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func inner() -> Result<int, string> {
return ok(10)
}
func outer() -> Result<int, string> {
let x = inner()?
return ok(x + 1)
}Runs `go` → 99
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Choice.cs::Result_ErrorShortCircuits topic: result status: verified
// verified behavior: Test.go(...) == 99
namespace Test
func half(n: int) -> Result<int, string> {
if n % 2 != 0 {
return error("odd")
}
return ok(n / 2)
}
func chain(n: int) -> Result<int, string> {
let a = half(n)?
let b = half(a)?
return ok(b)
}
func go() -> int {
let r = chain(6)
return r.IsError ? 99 : r.UnwrapOr(0)
}Runs `go` → 2
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Choice.cs::Result_OkPropagation topic: result status: verified
// verified behavior: Test.go(...) == 2
namespace Test
func half(n: int) -> Result<int, string> {
if n % 2 != 0 {
return error("odd")
}
return ok(n / 2)
}
func chain(n: int) -> Result<int, string> {
let a = half(n)?
let b = half(a)?
return ok(b)
}
func go() -> int {
let r = chain(8)
return r.UnwrapOr(0 - 1)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Combinator_Unwrap_Ok topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func f() -> Result<int, string> { return ok(7) }
func go() -> int { return f().Unwrap() }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Combinator_UnwrapOr_Error topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func f() -> Result<int, string> { return error("bad") }
func go() -> int { return f().UnwrapOr(-1) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Combinator_UnwrapOr_Ok topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func f() -> Result<int, string> { return ok(42) }
func go() -> int { return f().UnwrapOr(-1) }Runs `go` → "non-positive"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Pipeline_MiddleStageFails topic: result status: verified
// verified behavior: Test.go(...) == "non-positive"
namespace Test
func parse(s: string) -> Result<int, string> {
if s == "" { return error("empty") }
return ok(int.Parse(s))
}
func positive(n: int) -> Result<int, string> {
if n <= 0 { return error("non-positive") }
return ok(n)
}
func run(s: string) -> Result<int, string> {
let a = parse(s)?
let b = positive(a)?
return ok(b)
}
func go() -> string {
let r = run("0")
return r.IsError ? r.Error : "ok"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Pipeline_ThreeStageSuccess topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func parse(s: string) -> Result<int, string> {
if s == "" { return error("empty") }
return ok(int.Parse(s))
}
func positive(n: int) -> Result<int, string> {
if n <= 0 { return error("non-positive") }
return ok(n)
}
func doubleIt(n: int) -> Result<int, string> {
return ok(n * 2)
}
func run(s: string) -> Result<int, string> {
let a = parse(s)?
let b = positive(a)?
let c = doubleIt(b)?
return ok(c + 0)
}
func go() -> int {
let r = run("13")
return r.IsOk ? r.Value : -1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Propagation_ChainsThroughSuccess topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func parse(s: string) -> Result<int, string> {
if s == "" { return error("empty") }
return ok(int.Parse(s))
}
func chain(s: string) -> Result<int, string> {
let n = parse(s)?
return ok(n + 1)
}
func go() -> int {
let r = chain("42")
return r.IsOk ? r.Value : -1
}Runs `go` → "zero"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Propagation_SecondStageErrorWins topic: result status: verified
// verified behavior: Test.go(...) == "zero"
namespace Test
func step1(n: int) -> Result<int, string> {
return ok(n)
}
func step2(n: int) -> Result<int, string> {
if n == 0 { return error("zero") }
return ok(n + 10)
}
func pipeline(n: int) -> Result<int, string> {
let a = step1(n)?
let b = step2(a)?
return ok(b)
}
func go() -> string {
let r = pipeline(0)
return r.IsError ? r.Error : "ok"
}Runs `go` → "empty"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Propagation_ShortCircuitsOnError topic: result status: verified
// verified behavior: Test.go(...) == "empty"
namespace Test
func parse(s: string) -> Result<int, string> {
if s == "" { return error("empty") }
return ok(int.Parse(s))
}
func chain(s: string) -> Result<int, string> {
let n = parse(s)?
return ok(n + 1)
}
func go() -> string {
let r = chain("")
return r.IsError ? r.Error : "ok"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Propagation_TwoStageChain topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func step1(n: int) -> Result<int, string> {
if n == 0 { return error("zero") }
return ok(n * 2)
}
func step2(n: int) -> Result<int, string> {
return ok(n + 10)
}
func pipeline(n: int) -> Result<int, string> {
let a = step1(n)?
let b = step2(a)?
return ok(b)
}
func go() -> int {
let r = pipeline(5)
return r.IsOk ? r.Value : -1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_AccumulateOverList topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func parse(s: string) -> Result<int, string> {
return ok(int.Parse(s))
}
func sumAll(items: List<string>) -> Result<int, string> {
var total = 0
for s in items {
let n = parse(s)?
total += n
}
return ok(total)
}
func go() -> int {
let xs = ["1", "2", "3"]
let r = sumAll(xs)
return r.IsOk ? r.Value : -1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_BranchOnInvalid topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func validate(n: int) -> Result<int, string> {
if n < 0 { return error("negative") }
return ok(n)
}
func go() -> int {
let r = validate(-5)
if r.IsOk { return r.Value }
return -1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_BranchOnValidation topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func validate(n: int) -> Result<int, string> {
if n < 0 { return error("negative") }
return ok(n)
}
func go() -> int {
let r = validate(7)
if r.IsOk { return r.Value }
return -1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_DataPayloadThroughPropagation topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
data User { id: int, score: int }
func load(id: int) -> Result<User, string> {
if id == 0 { return error("missing") }
return ok(User { id: id, score: 7 })
}
func scoreOf(id: int) -> Result<int, string> {
let u = load(id)?
return ok(u.score)
}
func go() -> int {
let r = scoreOf(5)
return r.IsOk ? r.Value : -1
}Runs `go` → "bad"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_EarlyErrorStopsAccumulation topic: result status: verified
// verified behavior: Test.go(...) == "bad"
namespace Test
func parse(s: string) -> Result<int, string> {
if s == "x" { return error("bad") }
return ok(int.Parse(s))
}
func sumAll(items: List<string>) -> Result<int, string> {
var total = 0
for s in items {
let n = parse(s)?
total += n
}
return ok(total)
}
func go() -> string {
let xs = ["1", "x", "3"]
let r = sumAll(xs)
return r.IsError ? r.Error : "ok"
}Runs `go` → "bad"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_ErrorCarriesError topic: result status: verified
// verified behavior: Test.go(...) == "bad"
namespace Test
func parse(s: string) -> Result<int, string> {
return error("bad")
}
func go() -> string {
let r = parse("x")
return r.IsError ? r.Error : "ok"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_IsErrorTrueOnError topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func f() -> Result<int, string> { return error("e") }
func go() -> bool { return f().IsError }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_IsOkTrueOnOk topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func f() -> Result<int, string> { return ok(1) }
func go() -> bool { return f().IsOk }Runs `go` → "wrapped:empty"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_MapErrorMessageManually topic: result status: verified
// verified behavior: Test.go(...) == "wrapped:empty"
namespace Test
func parse(s: string) -> Result<int, string> {
if s == "" { return error("empty") }
return ok(int.Parse(s))
}
func wrap(s: string) -> Result<int, string> {
let r = parse(s)
if r.IsError { return error("wrapped:{r.Error}") }
return ok(r.Value)
}
func go() -> string {
let r = wrap("")
return r.IsError ? r.Error : "ok"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_OkCarriesDataValue topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
data User { id: int, age: int }
func load(id: int) -> Result<User, string> {
if id == 0 { return error("missing") }
return ok(User { id: id, age: 30 })
}
func go() -> int {
let r = load(1)
if r.IsOk { return r.Value.age }
return -1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_OkCarriesValue topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func parse(s: string) -> Result<int, string> {
return ok(42)
}
func go() -> int {
let r = parse("x")
return r.IsOk ? r.Value : -1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_StoredAndReread topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func produce(n: int) -> Result<int, string> {
return ok(n + 3)
}
func go() -> int {
let r = produce(30)
let s = produce(r.Value)
return s.IsOk ? r.Value : -1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_Errors.cs::Result_VoidLikeOkUnit topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func validate(n: int) -> Result<int, string> {
if n > 0 { return ok(n) }
return error("non-positive")
}
func go() -> int {
let r = validate(5)
return r.IsOk ? 1 : 0
}Runs `test` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Result_Err_Construction topic: result status: verified
// verified behavior: Test.test(...) == true
namespace Test
func produce(n: int) -> Result<int, string> {
if n < 0 {
return error("negative")
}
return ok(n)
}
func test() -> bool {
let r = produce(-1)
return r.IsError
}Runs `test` → -1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Result_IsOk_Branch topic: result status: verified
// verified behavior: Test.test(...) == -1
namespace Test
func produce(b: bool) -> Result<int, string> {
if b {
return ok(7)
}
return error("bad")
}
func test(b: bool) -> int {
let r = produce(b)
if r.IsOk {
return r.Value
}
return -1
}Runs `test` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Result_Map_Chain_Pin topic: result status: verified
// verified behavior: Test.test(...) == 42
namespace Test
func parse(s: string) -> Result<int, string> {
if s == "" {
return error("empty")
}
return ok(42)
}
func test() -> int {
let r = parse("hello")
return r.Value
}Runs `test` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Result_Ok_Construction_And_Unwrap topic: result status: verified
// verified behavior: Test.test(...) == 42
namespace Test
func produce(n: int) -> Result<int, string> = ok(n)
func test() -> int {
let r = produce(42)
return r.Value
}Showcase example
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: money.es topic: result status: verified
// hand-authored, idiomatic E# — verified through the E# compiler
namespace Demo
// ═════════════════════════════════════════════════════════════════════════════
// Money done right: a fixed-point amount in integer cents, never a float.
//
// Money is the textbook case for value semantics. An amount has no identity — two
// $5.00 bills are interchangeable — and you never want one assignment to mutate
// another ledger entry by surprise. So `Money` is a `data`: copied on assignment,
// equal by its field, no shared mutation. And it's stored as `cents: int`, because
// binary floating point cannot represent 0.10 exactly and money math must be exact.
//
// Every operation is a free function whose first parameter is `Money`, so by
// promotion it reads as a method on the value — `a.plus(b)`, `price.times(3)`,
// `total.format()` — and because each returns a fresh `Money` (or a `Result`), the
// calls chain without ever mutating the input. Division can fail (zero divisor), so
// it returns `Result<Money, string>` and the `?` operator threads the failure.
// ═════════════════════════════════════════════════════════════════════════════
// A value amount. One field, integer cents. No constructor — value `data` is built
// with a composite literal; a factory gives a readable surface.
data Money {
cents: int
}
// Factories. `dollars(5)` → $5.00; `of(12, 34)` → $12.34.
func dollars(d: int) -> Money = Money { cents: d * 100 }
func of(d: int, c: int) -> Money = Money { cents: d * 100 + c }
// --- promoted arithmetic (first param `Money` ⇒ method on `Money`) ----------------
// Each returns a new value; the receiver is never touched.
func plus(a: Money, b: Money) -> Money = Money { cents: a.cents + b.cents }
func minus(a: Money, b: Money) -> Money = Money { cents: a.cents - b.cents }
func times(a: Money, factor: int) -> Money = Money { cents: a.cents * factor }
func isNegative(a: Money) -> bool = a.cents < 0
// Splitting a bill N ways can fail (N <= 0), so it speaks `Result`. Integer division
// floors, so the remainder cents are handed back too — no money is silently lost.
func splitEvenly(a: Money, ways: int) -> Result<Money, string> {
if ways <= 0 {
return error("cannot split into {ways} shares")
}
return ok(Money { cents: a.cents / ways })
}
// Render as "$D.CC", padding the cents and handling a negative amount cleanly.
func format(a: Money) -> string {
let neg = a.cents < 0
let abs = neg ? 0 - a.cents : a.cents
let d = abs / 100
let c = abs % 100
let cc = c < 10 ? "0{c}" : "{c}"
let sign = neg ? "-" : ""
return "{sign}${d}.{cc}"
}
// A small end-to-end calculation: three items, a discount, split two ways.
// The `?` after splitEvenly unwraps the ok value or returns the error from here.
func checkout() -> Result<string, string> {
let apple = of(0, 99) // $0.99
let bread = of(2, 49) // $2.49
let coffee = dollars(8) // $8.00
let subtotal = apple.plus(bread).plus(coffee) // chained, no mutation: $11.48
let discount = of(1, 48) // $1.48 off
let total = subtotal.minus(discount) // $10.00
let perPerson = total.splitEvenly(2)? // $5.00, or propagate the error
return ok("total {total.format()}, each {perPerson.format()}")
}
func main() -> string {
let r = checkout()
return r.IsOk ? r.Value : "error: {r.Error}" // "total $10.00, each $5.00"
}
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_TryUnwrap_In_Let topic: result status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Prop
pub func inner(x: int) -> Result<int, string> {
return ok(x)
}
pub func outer(x: int) -> Result<int, string> {
let val = inner(x)?
return ok(val)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ErrorPropagationTests.cs::Question_AsBareStatement_Error_Propagates topic: result status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> Result<int, string> {
parse(-1)?
return ok(99) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ErrorPropagationTests.cs::Question_AsBareStatement_Ok_Continues topic: result status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> Result<int, string> {
parse(5)?
return ok(99) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ErrorPropagationTests.cs::Question_AsCallArgument topic: result status: unverified
// compiles cleanly (no auto-run claim was extracted)
func add(a: int, b: int) -> int = a + b
func go() -> Result<int, string> {
return ok(add(parse(3)?, 100)) // 6 + 100
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ErrorPropagationTests.cs::Question_ChainedStatements_StopAtFirstError topic: result status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> Result<int, string> {
parse(1)? // ok, discarded
parse(-9)? // error here — propagates, the rest never runs
return ok(0)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ErrorPropagationTests.cs::Question_InLetPosition_Ok topic: result status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> Result<int, string> {
let x = parse(5)?
return ok(x + 1) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ErrorPropagationTests.cs::Question_InReturnPosition_Ok topic: result status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> Result<int, string> { return ok(parse(5)? + 1) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ErrorPropagationTests.cs::Question_InReturnPosition_PropagatesError topic: result status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> Result<int, string> { return ok(parse(-3)? + 1) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ErrorPropagationTests.cs::Question_Nested topic: result status: unverified
// compiles cleanly (no auto-run claim was extracted)
func go() -> Result<int, string> { return ok(parse(parse(3)?)?) }