Skip to content

core — examples

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

Compiles

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

namespace Test

func try_parse(input: int, out doubled: int) -> bool {
    doubled = input * 2
    return true
}

Runs `go` → 6

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

namespace Test
func add3(a: int, b: int, c: int) -> int = a + b + c
func go() -> int = add3(
    1,
    2,
    3,
)

Runs `go` → 6

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

namespace Test
func add3(a: int, b: int, c: int) -> int = a + b + c
func go() -> int {
    let r = add3(
        1
        2
        3
    )
    return r
}

Runs `go` → 1

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

namespace Test
func one(
) -> int = 1
func go() -> int = one()

Runs `go` → 6

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

namespace Test
func add3(
    a: int,
    b: int,
    c: int,
) -> int = a + b + c
func go() -> int = add3(1, 2, 3)

Runs `go` → 10

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

namespace Test
func add4(a: int, b: int,
          c: int
          d: int) -> int = a + b + c + d
func go() -> int = add4(1, 2, 3, 4)

Runs `go` → 6

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

namespace Test
func add3(
    a: int
    b: int
    c: int
) -> int = a + b + c
func go() -> int = add3(1, 2, 3)

Runs `go` → 30

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

namespace Test
class Pair {
    a: int
    b: int
    init(
        a: int,
        b: int,
    ) {
        self.a = a
        self.b = b
    }
    func sum() -> int = self.a + self.b
}
func go() -> int {
    let p = Pair(10, 20)
    return p.sum()
}

Runs `go` → 9

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

namespace Test
interface ICombine {
    func combine(
        a: int,
        b: int,
    ) -> int
}
class Adder : ICombine {
    func combine(a: int, b: int) -> int = a + b
}
func go() -> int {
    let c = Adder()
    return c.combine(4, 5)
}

Runs `go` → 11

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

namespace Test
func go() -> int {
    let f: Func<int, int, int> = func(
        a: int,
        b: int,
    ) -> int { return a + b }
    return f(5, 6)
}

Rejected at compile time: ES2173

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

class Box {
    let item: Sym
    init(i: Sym) { self.item = i }
}
func touch() -> int = 0
func go() -> int {
    let b = Box(TypeSym("a", 4))
    if b.item is TypeSym {
        let z = touch()
        return b.item.arity
    }
    return 0
}

Compiles

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

namespace Test
func go() -> int {
    let x: int? = 5
    if x != nil { return x }
    return 0
}

Rejected at compile time: ES2173

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

func go() -> int {
    var s: Sym = TypeSym("a", 1)
    if s is TypeSym { return s.arity }
    return 0
}

Compiles

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

namespace Test

func add(a: int, b: int) -> int {
    var result = a + b
    return result
}

PdbTests__NoDebugSymbols_NoPdbFile

core unknown verified ×2

Compiles

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

namespace Test

func run() -> int {
    return 42
}

PdbTests__Pdb_HasSequencePoints

core unknown verified

Compiles

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

namespace Test

func compute(x: int) -> int {
    var a = x + 1
    var b = a * 2
    return b
}

Compiles

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

namespace Test

class Svc(store: int, maxRetries: int) {
    var tokens: int
    init { tokens = maxRetries }
    func read() -> int { return store }
}

func run() -> int {
    let s = Svc(7, 3)
    return s.read() + s.tokens
}

Compiles

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

namespace Test

class Greeter(name: string) {
    func greet() -> string { return "hi " + name }
}

func run() -> string {
    let g = Greeter("kae")
    return g.greet()
}

Compiles

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

namespace Test

class Adder(amount: int) {
    func addOwn(v: int) -> int { return v + amount }
    func addOther(v: int, amount: int) -> int { return v + amount }
}

func run() -> int {
    let a = Adder(100)
    return a.addOwn(1) * 1000 + a.addOther(1, 5)
}

Compiles

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

namespace Test

class Counter(step: int) {
    func up(v: int) -> int { return v + step }
    func down(v: int) -> int { return v - step }
}

func run() -> int {
    let c = Counter(5)
    return c.up(10) + c.down(10)
}

Rejected at compile time: ES2190

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

namespace Test

class Svc(a: int) {
    var b: int
}

func run() -> Svc {
    return Svc { b: 1 }
}

Compiles

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

namespace Test

class Conn(host: string, port: int = 80) {
    func describe() -> string { return host + ":" + port.ToString() }
}

func run() -> string {
    let a = Conn("x")
    let b = Conn(port: 9, host: "y")
    return a.describe() + " " + b.describe()
}

Compiles

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

namespace Test

class Svc(maxRetries: int) {
    var tokens: int = maxRetries
    init {
        if maxRetries <= 0 { tokens = 1 }
    }
    func budget() -> 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: PrimaryCtorCaptureTests.cs::Header_FieldDefaultReadsHeaderParam   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

class Retry(maxRetries: int) {
    var tokens: int = maxRetries
}

func run() -> int {
    let r = Retry(9)
    return r.tokens
}

Rejected at compile time: ES2188

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

namespace Test

class Svc(count: int) {
    var count: int
}

Compiles

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

namespace Test

class Box(
    width: int
    height: int,
) {
    func area() -> int { return width * height }
}

func run() -> int {
    let b = Box(
        3,
        4,
    )
    return b.area()
}

Compiles

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

namespace Test

class Marker(id: int)

func run() -> int {
    let m = Marker(3)
    return 1
}

Rejected at compile time: ES2185

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

namespace Test

class Svc(a: int) {
    init(b: string) : this(1) { }
}

Compiles

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

namespace Test

class Conn(host: string, port: int) {
    var dialed: bool
    init(host: string) : this(host, 80) { dialed = true }
    func describe() -> string { return host + ":" + port.ToString() }
}

func run() -> string {
    let c = Conn("z")
    var s = c.describe()
    if c.dialed { s = s + "!" }
    return s
}

Rejected at compile time: ES2186

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

namespace Test

class Svc(a: int) {
    init(b: string) { }
}

Compiles

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

namespace Test

func pair() -> (int, int) {
    return (3, 4)
}

func run() -> int {
    let (a, b) = pair()
    return a + b
}

Compiles

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

namespace Test

class Tag {
    required let id: int
}

func run() -> int {
    let t = Tag { id: 5 }
    return t.id
}

Rejected at compile time: ES2189

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

namespace Test

class Options {
    required var name: string
    var verbose: bool
}

func run() -> Options {
    return Options { verbose: true }
}

Compiles

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

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

Compiles

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

namespace Test
func add(a: int, b: int) -> int = a + b
func go(x: int) -> int = add(x, 2)

Compiles

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

namespace Test
func go() -> int {
    let msgs = List<string>()
    msgs.Add("hi")
    return msgs.Count
}

Compiles

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

namespace Test
func helper() -> int = 42
func a() -> int = helper()
func b() -> int = helper() + helper()

Compiles

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

namespace Test
func go() -> int {
    let total = 10
    return total + total
}

Compiles

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

namespace Test
class Counter {
    n: int
    init(n: int) { self.n = n }
    func bump(by: int) -> int {
        return self.n + by
    }
}

Compiles

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

namespace Test
func go(seed: int) -> int {
    let mid = seed + 1
    let last = mid + 1
    return last
}

Compiles

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

namespace Test

func run() -> int {
    let x = 42
    return x
}

Compiles

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

namespace Test

func check(x: int) -> int {
    if x > 0 {
        return x
    }
    return 0
}

Compiles

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

namespace Test

func add(a: int, b: int) -> int {
    var total = a
    total += b
    return total
}

Runs `go` → "line one\nline two"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: StringLiteralFormTests.cs::RawString_MultiLine_StripsIndentAndFenceNewlines   topic: core   status: verified
// verified behavior: Test.go(...) == "line one\nline two"

namespace Test
func go() -> string {
    return """
        line one
        line two
        """
}

Rejected at compile time: ES2146

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

func main() -> int {
    let v = missing.field + 1
    return v
}

Rejected at compile time: ES2146

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

func main() -> int {
    return undefinedThing + 1
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::BoxingDiag_NoWarn_RefData   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 test() -> string {
    let w: IWidget = Button { label: "ok" }
    return w.render()
}

Compiles

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

namespace Cfg

class Config {
    timeout: int
}

Compiles

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

namespace Test

interface IRenderable {
    func render() -> string
}

class Button : IRenderable {
    label: string
}

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

Compiles

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

namespace Test

[HttpGet]
func getUsers() -> string {
    return "users"
}