core — examples
← all topics · 698 examples · page 1 of 14 · raw source ↓
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorDelegation_FieldDefaults_NotDoubleApplied topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Tally {
var hits: int = 1
init(bump: int) { hits = hits + bump }
init() : this(10) { hits = hits + 100 }
}
func run() -> int {
let t = Tally()
return t.hits
}Rejected at compile time: ES2187
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorDelegation_MutualCycle_ES2187 topic: core status: verified
// verified behavior: reports diagnostic ES2187
namespace Test
class Loop {
var v: int
init(a: int) : this(a, 1) { v = a }
init(a: int, b: int) : this(a) { v = a + b }
}Rejected at compile time: ES2187
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorDelegation_SelfCycle_ES2187 topic: core status: verified
// verified behavior: reports diagnostic ES2187
namespace Test
class Loop {
var v: int
init(a: int) : this(a) { v = a }
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorDelegation_ThisAsCall_NotNewobj topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Box {
var v: int
init(v: int) { self.v = v }
init() : this(9) { }
}
func touch() -> int {
let b = Box()
return b.v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorDelegation_ThisChain_RunsDelegateThenBody topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Buf {
var cap: int
var labeled: bool
init(cap: int) { self.cap = cap }
init() : this(16) { labeled = true }
}
func run() -> int {
let b = Buf()
var r = b.cap
if b.labeled { r = r + 1 }
return r
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorDelegation_TwoHopChain_Runs topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Chain {
var trace: int
init(a: int, b: int) { trace = a * 10 + b }
init(a: int) : this(a, 2) { trace = trace + 100 }
init() : this(1) { trace = trace + 1000 }
}
func run() -> int {
let c = Chain()
return c.trace
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorOverload_DefaultParam_MaterializedAtConstruction topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Retry {
var max: int
var delay: int
init(max: int, delay: int = 250) {
self.max = max
self.delay = delay
}
}
func run() -> int {
let a = Retry(3)
let b = Retry(3, 10)
return a.delay + b.delay
}Rejected at compile time: ES2185
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorOverload_DuplicateArity_ES2185 topic: core status: verified
// verified behavior: reports diagnostic ES2185
namespace Test
class Dup {
var v: int
init(a: int) { v = a }
init(b: int) { v = b }
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorOverload_FieldDefaults_RunInEveryNonDelegatingCtor topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Widget {
var tag: int = 42
var size: int
init(size: int) { self.size = size }
init(size: int, scale: int) { self.size = size * scale }
}
func run() -> int {
let a = Widget(2)
let b = Widget(2, 3)
return a.tag + b.tag + a.size + b.size
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorOverload_NamedArgs_SelectByNames topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Cfg {
var v: int
init(host: int, port: int) { v = host * 1000 + port }
init(port: int) { v = port }
}
func run() -> int {
let a = Cfg(port: 80, host: 2)
let b = Cfg(port: 9)
return a.v + b.v
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorOverload_TwoArities_BothConstruct topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Point {
let x: int
let y: int
init(x: int, y: int) {
self.x = x
self.y = y
}
init(v: int) {
self.x = v
self.y = v
}
}
func run() -> int {
let a = Point(3, 4)
let b = Point(7)
return a.x * 100 + a.y * 10 + b.x + b.y - 7
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorOverload_ZeroArgInit_NoSecondImplicitParameterlessCtor topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Conn {
var live: bool
init() { live = true }
}
func touch() -> int {
let c = Conn()
return 1
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorOverload_ZeroArgInit_ServesAsParameterlessCtor topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Counter {
var n: int
init() { n = 100 }
init(start: int) { n = start }
}
func run() -> int {
let a = Counter()
let b = Counter(5)
return a.n + b.n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::CtorVisibility_PrivInit_EmitsPrivateCtor topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Singleton {
var v: int
priv init(v: int) { self.v = v }
init() : this(7) { }
}
func run() -> int {
let s = Singleton()
return s.v
}Rejected at compile time: ES2180
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Default_CallExpression_IsES2180 topic: core status: verified
// verified behavior: reports diagnostic ES2180
namespace Test
func now() -> int = 1
func f(x: int = now()) -> int = xRuns `go` → 64
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Default_Expression_Arithmetic topic: core status: verified
// verified behavior: Test.go(...) == 64
namespace Test
func bufSize(kb: int = 8 * 8) -> int = kb
func go() -> int = bufSize()Runs `go` → -1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Default_NegativeLiteral topic: core status: verified
// verified behavior: Test.go(...) == -1
namespace Test
func find(sentinel: int = -1) -> int = sentinel
func go() -> int = find()Rejected at compile time: ES2180
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Default_ReferencingPriorParam_IsES2180 topic: core status: verified
// verified behavior: reports diagnostic ES2180
namespace Test
func f(a: int, b: int = a) -> int = a + bRejected at compile time: ES2184
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::DuplicateArg_IsES2184 topic: core status: verified
// verified behavior: reports diagnostic ES2184
namespace Test
func sub(a: int, b: int) -> int = a - b
func go() -> int = sub(1, a: 2)Runs `go` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::External_OmittedOptional_Fills topic: core status: verified
// verified behavior: Test.go(...) == 3
namespace Test
func go() -> int {
let parts = "a,b,,c".Split(",", StringSplitOptions.RemoveEmptyEntries)
return parts.Length
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::LiteralDefault_Emits_OptionalConstant topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func connect(host: string, port: int = 8080) -> int = portRuns `go` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Named_AllNamed_NoDefaults topic: core status: verified
// verified behavior: Test.go(...) == 7
namespace Test
func sub(a: int, b: int) -> int = a - b
func go() -> int = sub(b: 3, a: 10)Runs `go` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Named_OnClassInit topic: core status: verified
// verified behavior: Test.go(...) == 42
namespace Test
class Server {
port: int
init(port: int = 42) { self.port = port }
func get() -> int = self.port
}
func go() -> int {
let s = Server()
return s.get()
}Rejected at compile time: ES2180
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::NonConstant_Default_IsES2180 topic: core status: verified
// verified behavior: reports diagnostic ES2180
namespace Test
var seed: int = 1
func f(x: int = seed) -> int = xRejected at compile time: ES2180
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::OutParam_Default_IsES2180 topic: core status: verified
// verified behavior: reports diagnostic ES2180
namespace Test
func f(out r: int = 3) { r = 1 }Rejected at compile time: ES2181
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Positional_AfterNamed_IsES2181 topic: core status: verified
// verified behavior: reports diagnostic ES2181
namespace Test
func sub(a: int, b: int) -> int = a - b
func go() -> int = sub(a: 1, 2)Rejected at compile time: ES2183
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::UnfilledParam_IsES2183 topic: core status: verified
// verified behavior: reports diagnostic ES2183
namespace Test
func sub(a: int, b: int) -> int = a - b
func go() -> int = sub(b: 2)Rejected at compile time: ES2182
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::UnknownName_IsES2182 topic: core status: verified
// verified behavior: reports diagnostic ES2182
namespace Test
func sub(a: int, b: int) -> int = a - b
func go() -> int = sub(1, c: 2)Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: EmitterStressTests.cs::NullConditionalChainWithCoalesce topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func safe(s: string) -> int {
return s?.Length ?? 0
}
func ternaryChain(x: int) -> string {
let label = x > 100 ? "high" : x > 50 ? "mid" : "low"
return label
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: EmitterStressTests.cs::TryCatchWithTypedBinding topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() -> string {
try {
let x = 1 / 0
return "ok"
} catch (Exception ex) {
return ex.Message
}
}Runs `check` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Char_LetThenCompare topic: core status: verified
// verified behavior: Test.check(...) == false
namespace Test
func check(s: string, i: int) -> bool {
let target = 'X'
return s[i] == target
}Runs `isNewline` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Char_LiteralEqualityAgainstStringIndex topic: core status: verified
// verified behavior: Test.isNewline(...) == true
namespace Test
func isSpace(s: string, i: int) -> bool {
return s[i] == ' '
}
func isQuote(s: string, i: int) -> bool {
return s[i] == '"'
}
func isNewline(s: string, i: int) -> bool {
return s[i] == '\n'
}Runs `isLetterAt` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Char_PassedToBclMethod topic: core status: verified
// verified behavior: Test.isLetterAt(...) == false
namespace Test
func isLetterAt(s: string, i: int) -> bool {
return char.IsLetter(s[i])
}
func isDigitAt(s: string, i: int) -> bool {
return char.IsDigit(s[i])
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FluentChainTests.cs::Chain_PreservesIdentity_SameObject topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Acc {
var n: int
init() { self.n = 0 }
}
func (a: Acc) add(x: int) -> Acc {
a.n += x
return a
}
func go() -> int {
let a = Acc()
let b = a.add(40).add(4)
return a.n + b.n // 44 + 44 — a and b are the same object
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FluentChainTests.cs::RefData_SelfReturn_ChainsOnOneLine topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Acc {
var n: int
init() { self.n = 0 }
}
func (a: Acc) add(x: int) -> Acc {
a.n += x
return a
}
func go() -> int {
let a = Acc().add(5).add(3).add(2)
return a.n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FluentChainTests.cs::RefData_SelfReturn_MultiLineLeadingDot topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Acc {
var n: int
init() { self.n = 0 }
}
func (a: Acc) add(x: int) -> Acc {
a.n += x
return a
}
func go() -> int {
let a = Acc()
.add(5)
.add(3)
.add(2)
return a.n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FuzzFindingsTests.cs::Emit_IsDeterministic_AcrossTwoCompilationsInOneProcess topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func cls1(x: int, m: int) -> int {
return ((a) => a * 5 + m)(x) + ((b) => b + 12)(m)
}
func cls2(y: int) -> int {
return ((c) => c * 2)(y) + ((d) => d - 1)(y)
}
func go() -> int {
return cls1(3, 4) + cls2(10)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FuzzFindingsTests.cs::IntLiteralAtUlongMax_StillParses topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> uint64 { return 18446744073709551615 }Runs `go` → 27
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FuzzFindingsTests.cs::LambdaIife_RunsBody_ReturnsResult topic: core status: verified
// verified behavior: Test.go(...) == 27
namespace Test
func go() -> int {
return ((b) => b + 24)(3)
}Runs `go` → -4
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FuzzFindingsTests.cs::LambdaIife_WithCapture_RunsBody topic: core status: verified
// verified behavior: Test.go(...) == -4
namespace Test
func go() -> int {
let m = 3
return ((a) => a * 7 + m)(0 - 1)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FuzzFindingsTests.cs::Printer_ClassMethod_NotDuplicatedAtTopLevel topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Svc(maxRetries: int) {
tokens: int = maxRetries
func budget(self: Svc) -> int {
return self.tokens
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Attributes_OnDataType_EmittedAsCLRAttributes topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
[Serializable]
class Config {
name: string
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Attributes_OnFunction_EmittedAsCLRAttributes topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
[Obsolete]
func oldMethod() -> int {
return 0
}Runs `run` → 15
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ByRef_MutatesThroughPointer topic: core status: verified
// verified behavior: Test.run(...) == 15
namespace Test
func addTen(x: *int) {
x += 10
}
func run() -> int {
var n = 5
addTen(*n)
return n
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Closure_CapturedLet_ReadsCorrectly topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func makeAdder(base: int) -> int {
let offset = 100
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: ILEmitterTests.cs::Closure_CapturedVar_MutationPropagates topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func evalWith(value: int, offset: int) -> int {
var total = 0
let addTo = func(v: int) { total = total + v }
addTo(value)
addTo(offset)
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Closure_NoCaptureIL_EmitsDelegate topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func apply(f: int, g: int) -> int {
let double = func(x: int) -> int { return x * 2 }
return double(f)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::CompoundAssignment_IL_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func accumulate(n: int) -> int {
var total = 0
total += n
total += n
total -= 1
return total
}Runs `run` → false
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Default_BoolIsFalse topic: core status: verified
// verified behavior: Test.run(...) == false
namespace Test
func run() -> bool {
let b = default(bool)
return b
}Runs `run` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Default_IntIsZero topic: core status: verified
// verified behavior: Test.run(...) == 0
namespace Test
func run() -> int {
let x = default(int)
return x
}