Skip to content

core — examples

← all topics · 698 examples · page 12 of 14 · raw source ↓

Compiles

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

namespace Test

[Route("/api/users")]
[Authorize]
class UserController {
    path: string
}

Compiles

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

namespace Test

[MaxLength(100)]
class Config {
    name: string
}

Compiles

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

namespace Test

func make_adder(base: int) -> int {
    let offset = 10
    let add = func(x: int) -> int { return offset + x }
    return add(base)
}

Compiles

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

namespace Test

func counter() -> int {
    var count = 0
    let increment = func() { count += 1 }
    increment()
    increment()
    return count
}

Compiles

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

namespace Math

pub func sumTo(n: int) -> int {
    var total = 0
    var i = 0
    while i <= n {
        total += i
        i += 1
    }
    return total
}

Compiles

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

namespace Arr

pub func first(items: List<int>) -> int {
    return items[0]
}

Compiles

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

namespace Last

pub func last(items: List<int>) -> int {
    return items[^1]
}

Compiles

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

namespace Test

class Connection {
    host: string
    port: int

    init(h: string, p: int) {
        host = h
        port = p
    }
}

Compiles

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

namespace Test

func process(name: string?) -> int {
    return 0
}

Compiles

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

namespace Plain

pub func msg() -> string {
    return "hello world"
}

Compiles

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

namespace Slice

pub func take(s: string, n: int) -> string {
    return s[..n]
}

pub func skip(s: string, n: int) -> string {
    return s[n..]
}

pub func sub(s: string, a: int, b: int) -> string {
    return s[a..b]
}

Compiles

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

namespace Worker

pub func sumTo(limit: int) -> int {
    var i = 0
    var total = 0
    while i <= limit {
        total = total + i
        i = i + 1
    }
    return total
}

pub func sumAll(values: List<int>) -> int {
    var total = 0
    for value in values {
        total = total + value
    }
    return total
}

pub func start(values: List<string>) -> Spawned {
    return spawn {
        for value in values {
            Console.WriteLine(value)
        }
    }
}

Compiles

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

namespace Test

interface IWidget {
    func render() -> string
}

class Button : IWidget {
    label: string
}

func (b: Button) render() -> string {
    return b.label
}

func display(w: IWidget) -> string {
    return w.render()
}

Compiles

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

namespace Test
func go() -> int {
    try {
        throw InvalidOperationException("boom")
    } catch (InvalidOperationException e) {
        return 3
    }
    return 0
}

Compiles

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

namespace Test
func go() -> int {
    let n = default(int)
    let s = default(string)
    if n == 0 && s == nil { return 1 }
    return 0
}

Compiles

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

namespace Test
func two(x: bool, y: bool) -> int {
    if x && y { return 1 }
    return 0
}

func go() -> int {
    let a = 1
    let b = 2
    let c = 3
    let d = 2
    return two(a < b, c > d)
}

Compiles

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

namespace T
func f(x: var) { }

Compiles

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

namespace Test
func total(xss: List<List<int>>) -> int {
    var sum = 0
    for xs in xss {
        for x in xs {
            sum = sum + x
        }
    }
    return sum
}

func go() -> int {
    var inner1 = List<int>()
    inner1.Add(1)
    inner1.Add(2)
    var inner2 = List<int>()
    inner2.Add(3)
    var xss = List<List<int>>()
    xss.Add(inner1)
    xss.Add(inner2)
    return total(xss)
}

Compiles

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

namespace Test
func maybe(x: int?) -> int {
    if x == nil { return 1 }
    return 2
}

func go() -> int { return maybe(7) }

Compiles

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

namespace Test
func pair() -> (int, string) { return (5, "abc") }

func go() -> int {
    let t = pair()
    return t.Item1 + t.Item2.Length
}

Compiles

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

namespace Test

class Lost {
    var v: int
    init(a: int) : this(a, 2, 3) { v = a }
}

Compiles

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

namespace Test

class P {
    var x: int
    init(x: int) { self.x = x }
    init(x: int, y: int) { self.x = x + y }
}

func run() -> int {
    let p = P(1, 2, 3)
    return p.x
}

Runs `go` → "localhost:8080/5000/tls"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::AllDefaults   topic: core   status: unverified
// verified behavior: Test.go(...) == "localhost:8080/5000/tls"

func go() -> string = connect("localhost")

Runs `go` → "h:9090/250/tls"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Named_OutOfOrder   topic: core   status: unverified
// verified behavior: Test.go(...) == "h:9090/250/tls"

func go() -> string = connect("h", timeoutMs: 250, port: 9090)

Runs `go` → "h:8080/5000/plain"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Named_SkipMiddle   topic: core   status: unverified
// verified behavior: Test.go(...) == "h:8080/5000/plain"

func go() -> string = connect("h", useTls: false)

Runs `go` → "h:9090/5000/tls"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::TrailingDefaults_PositionalPrefix   topic: core   status: unverified
// verified behavior: Test.go(...) == "h:9090/5000/tls"

func go() -> string = connect("h", 9090)

Compiles

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

namespace T

class Plugin {
    name: string
    init() { self.name = "test" }
    pub func Name() -> string = self.name
    pub func Run(app: WebApplication) {
        app.MapGet("/api/test", func(q: string) -> IResult {
            return Results.Json(q ?? "default")
        })
        app.MapGet("/api/ping", func() -> IResult = Results.Json("pong"))
    }
}

Compiles

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

namespace T
func run() -> int {
    let pub = 42
    return pub
}

Compiles

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

namespace Test
func go() -> int {

Compiles

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

namespace Test
func go() -> int {
    return 99999999999999999999999999999999999999999999999999999999999999999999999999999999
}

Compiles

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

namespace Test
func go() -> int {
  let x = 

Compiles

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

namespace Test
class Svc(maxRetries: int) {
    tokens: int = maxRetries
    init() {
        if maxRetries <= 0 {
            tokens = 1
        }
    }
    func budget(self: Svc) -> int {
        return self.tokens + maxRetries
    }
}
func run() -> int {
    let bad = Svc(0)
    let good = Svc(4)
    return (bad.budget() * 100) + good.budget()
}

Compiles

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

namespace Test

func bad() -> int {
    let x = 1
    x = 2
    return x
}

Compiles

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

namespace Test

func safeLen(s: string) -> string {
    return s?.Length ?? "0"
}

Compiles

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

namespace Test

class Foo {
    x: int
    lock: object

    init() {
        self.x = 0
        self.lock = object()
    }

    func getX() -> int = self.x
}

Compiles

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

namespace T
class MyPlugin : IPlugin {
    init() { }
    pub func Name() -> string = "test"
    pub func Run(app: WebApplication) {
        app.MapGet("/api/test", func(q: string) -> IResult {
            return Results.Json(q ?? "")
        })
        app.MapGet("/api/ping", func() -> IResult = Results.Json("pong"))
    }
}

Compiles

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

namespace T
func run() {
    let items = List<string>()
    for item in items.Where(func(s: string) -> bool { return s != "" }).ToList() {
        let x = item
    }
}

Compiles

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

namespace T
class Svc {
    items: List<string>
    obj: object
    init() {
        self.items = List<string>()
        self.obj = object()
    }
    func start() { self.items.Add("a") }
    func stop() { self.items.Clear() }
    func query(filter: string) -> List<string> { return self.items }
    func count() -> int = self.items.Count
    func addItem(s: string) = self.items.Add(s)
}

Rejected at compile time: ES2001

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::StructPromotion_RefData_NoWarning   topic: core   status: unverified
// verified behavior: reports diagnostic ES2001

namespace Test

class Big {
    a: double
    b: double
    c: double
    d: double
    e: double
    f: double
    g: double
    h: double
    i: double
}

Compiles

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

namespace Test

class Node {
    value: int
}

func run() -> int {
    let n = Node { value: 1 }
    let m = n with { value: 2 }
    return m.value
}

Compiles

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

namespace Test
func go() -> int {
  return 

Runs `go` → true

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

func same(a: Pair<int, int>, b: Pair<int, int>) -> bool = a.Equals(b)
func go() -> bool {
  let x = Pair<int, int> { first: 9, second: 9 }
  let y = Pair<int, int> { first: 9, second: 9 }
  return same(x, y)
}

Runs `go` → true

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

func go() -> bool {
  let a = Pair<int, int> { first: 7, second: 8 }
  let b = Pair<int, int> { first: 7, second: 8 }
  return a.GetHashCode() == b.GetHashCode()
}

Runs `go` → true

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

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

Runs `go` → false

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

func go() -> bool {
  let a = Pair<int, int> { first: 3, second: 4 }
  let b = Pair<int, int> { first: 3, second: 5 }
  return a.Equals(b)
}

Runs `go` → true

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

func go() -> bool {
  let a = Pair<string, bool> { first: "y", second: true }
  let b = Pair<string, bool> { first: "y", second: true }
  return a.Equals(b)
}

Runs `go` → true

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

func go() -> bool {
  let a = Pair<int, int> { first: 1, second: 1 }
  let b = Pair<int, int> { first: 1, second: 1 }
  return a.Equals(b)
}

Runs `go` → true

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

func go() -> bool {
  let a = Pair<string, int> { first: "x", second: 1 }
  let b = Pair<string, int> { first: "x", second: 1 }
  return a.Equals(b)
}

Runs `go` → false

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

func go() -> bool {
  let a = Pair<string, int> { first: "x", second: 1 }
  let b = Pair<string, int> { first: "y", second: 1 }
  return a.Equals(b)
}

Runs `go` → true

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

func ints() -> bool {
  let a = Pair<int, int> { first: 1, second: 2 }
  let b = Pair<int, int> { first: 1, second: 2 }
  return a.Equals(b)
}