core — examples
← all topics · 698 examples · page 2 of 14 · raw source ↓
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::DelegateInvoke_ActionVoid_IL_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func runCallback(x: int) -> int {
var result = 0
let cb = func(v: int) { result = v * 2 }
cb(x)
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::DeriveEquality_RefData_WorksWithDifferentInstances topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
derive equality
class Config {
name: string
port: int
init(name: string, port: int) {
self.name = name
self.port = port
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Emit_LambdaWithTupleReturnType topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
var tasks = List<Task<(List<string>, List<string>)>>()
tasks.Add(Task.Run(func() -> (List<string>, List<string>) { return (List<string>(), List<string>()) }))
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::EntryPoint_Transpiler_EmitsMain topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func main() {
var x = 42
}Runs `run` → 2
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Event_Subscribe_ObservableCollectionCollectionChanged topic: core status: verified
// verified behavior: Test.run(...) == 2
namespace Test
func run() -> int {
var fired = 0
let coll = ObservableCollection<int>()
coll.CollectionChanged += func(sender: object, args: NotifyCollectionChangedEventArgs) -> void {
fired = fired + 1
}
coll.Add(10)
coll.Add(20)
return fired
}Runs `always42` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ExpressionBodied_SimpleReturn topic: core status: verified
// verified behavior: Test.always42(...) == 42
namespace Test
func double(x: int) -> int = x * 2
func negate(x: int) -> int = 0 - x
func always42() -> int = 42Runs `run` → 21
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ExpressionBodiedMethodInRefData topic: core status: verified
// verified behavior: Test.run(...) == 21
namespace Test
class Box {
value: int
init(v: int) { self.value = v }
func get() -> int = self.value
func doubled() -> int = self.value * 2
}
func run() -> int {
let b = Box(7)
return b.get() + b.doubled()
}Runs `run` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Extension_LinqCount_OnList topic: core status: verified
// verified behavior: Test.run(...) == 3
namespace Test
func run() -> int {
let nums = List<int>()
nums.Add(1)
nums.Add(2)
nums.Add(3)
return nums.Count()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ExternalCall_ConsoleWriteLine_IL topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func main() {
Console.WriteLine("hello")
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::FunctionPointer_Ldftn_EmitsCorrectOpcode topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func double(x: int) -> int {
return x * 2
}
func getDoublePtr() -> int {
let ptr = &double
return 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::FunctionPointer_TranspilerEmitsDelegateStar topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func double(x: int) -> int {
return x * 2
}
func getPtr() -> int {
let ptr = &double
return 0
}Runs `run` → 2
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericCall_EnumerableCountOnList topic: core status: verified
// verified behavior: Test.run(...) == 2
namespace Test
func run() -> int {
let nums = List<int>()
nums.Add(10)
nums.Add(20)
return Enumerable.Count<int>(nums)
}Runs `run` → 6
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericCall_InferenceStillWorks topic: core status: verified
// verified behavior: Test.run(...) == 6
namespace Test
func run() -> int {
let nums = List<int>()
nums.Add(1)
nums.Add(2)
nums.Add(3)
return nums.Sum()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericConstructorCall_List_ParsesCorrectly topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func make() -> int {
let items = List<string>()
return 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericExternalType_Runtime_ListAdd topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func run() -> int {
var count = 0
count = 3
return count
}Runs `run` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericTypeWithTupleArg_NoStackOverflow topic: core status: verified
// verified behavior: Test.run(...) == 0
namespace Test
func run() -> int {
var xs = List<(int, int)>()
return xs.Count
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::IL_Diagnostics_UnresolvedType_ReportsError topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func broken(x: Nonexistent) -> int {
return 0
}Runs `run` → 99
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Indexer_ListGetSet_RoundTrip topic: core status: verified
// verified behavior: Test.run(...) == 99
namespace Test
func run() -> int {
let list = List<int>()
list.Add(10)
list.Add(20)
list.Add(30)
list[1] = 99
return list[1]
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Init_Constructor_IL_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Point {
x: int
y: int
init(px: int, py: int) {
x = px
y = py
}
}
func getX() -> int {
let p = Point { x: 0, y: 0 }
return p.x
}Runs `runFound` → "found"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::LetElse_ReturnsOnNull topic: core status: verified
// verified behavior: Test.runFound(...) == "found"
namespace Test
func tryFind(key: string) -> string? {
if key == "a" {
return "found"
}
return nil
}
func run() -> string {
let value = tryFind("b") else {
return "default"
}
return value
}
func runFound() -> string {
let value = tryFind("a") else {
return "default"
}
return value
}Runs `first` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ListLiteral_IntElements topic: core status: verified
// verified behavior: Test.first(...) == 42
namespace Test
func count() -> int {
let xs = [10, 20, 30]
return xs.Count
}
func first() -> int {
let xs = [42, 99]
return xs[0]
}Runs `joined` → "hello"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ListLiteral_StringElements topic: core status: verified
// verified behavior: Test.joined(...) == "hello"
namespace Test
func joined() -> string {
let xs = ["hello", "world"]
return xs[0]
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::MultiFile_DistinctNamespaces_CoexistInOneAssembly topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Alpha
func a() -> int { return 1 }Runs `run` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::NestedGenericWithTupleArg_NoStackOverflow topic: core status: verified
// verified behavior: Test.run(...) == 0
namespace Test
func run() -> int {
var xs = List<(List<int>, List<int>)>()
return xs.Count
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::NoAwait_Binder_HasAwaitIsFalse topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func add(a: int, b: int) -> int {
return a + b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Nullable_NilReturn_ReferenceType_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func tryFind(s: string) -> string? {
return nil
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Nullable_NilReturn_ValueType_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func tryParse(s: string) -> int? {
return nil
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Nullable_ValueType_ResolvesToSystemNullable topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func maybe(x: int?) -> int {
return 0
}Runs `fallback` → "hello"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::NullCoalescing_StringFallback topic: core status: verified
// verified behavior: Test.fallback(...) == "hello"
namespace Test
func fallback(s: string) -> string {
return s ?? "default"
}Runs `safeLen` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::NullConditional_ChainedWithCoalescing topic: core status: verified
// verified behavior: Test.safeLen(...) == 5
namespace Test
func safeLen(s: string) -> int {
let result = s ?? ""
return result.Length
}
func withCoalesce(s: string) -> string {
return s ?? "none"
}Runs `run` → -1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Out_IntTryParse_Failure topic: core status: verified
// verified behavior: Test.run(...) == -1
namespace Test
func run() -> int {
if int.TryParse("not a number", out var n) {
return n
}
return -1
}Runs `run` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Out_IntTryParse_Success topic: core status: verified
// verified behavior: Test.run(...) == 42
namespace Test
func run() -> int {
if int.TryParse("42", out var n) {
return n
}
return -1
}Runs `run` → "hello world"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Params_StringFormat_NoExtraArgs topic: core status: verified
// verified behavior: Test.run(...) == "hello world"
namespace Test
func run() -> string {
return string.Format("hello {0}", "world")
}Runs `run` → "1 + 2 = 3"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Params_StringFormat_TwoArgs topic: core status: verified
// verified behavior: Test.run(...) == "1 + 2 = 3"
namespace Test
func run() -> string {
return string.Format("{0} + {1} = {2}", 1, 2, 3)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_ForTupleDestructuring topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
let results = List<(int, int)>()
for (a, b) in results {
let x = a + b
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_ImportStaticAndArrowLambda topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
using static "System.Math"
func run() -> double {
return Max(1.0, ((x) => x + 2.0)(3.0))
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_LambdaWithComplexGenericReturnType topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
var xs = List<Task<(List<string>, List<string>)>>()
xs.Add(Task.Run(func() -> (List<string>, List<string>) { return (List<string>(), List<string>()) }))
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_NullConditionalAndCoalesceInTernary topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
let s: string = nil
let x = s?.Length ?? 0
let y = x > 0 ? s : "default"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_TryCatchWithTypedBinding topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
func run() {
try {
let x = 1
} catch (Exception ex) {
let msg = ex.Message
}
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Parse_TupleTypeInGeneric topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func test() -> int {
var tasks = List<Task<(int, string)>>()
return tasks.Count
}Runs `make` → "hello"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::PositionalClass_IsCaptureHeader topic: core status: verified
// verified behavior: Test.make(...) == "hello"
namespace Test
class Label(text: string, size: int) {
func getText() -> string {
return text
}
}
func make() -> string {
let l = Label("hello", 12)
return l.getText()
}Runs `run` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Property_GetFromBclObject_ListCount topic: core status: verified
// verified behavior: Test.run(...) == 3
namespace Test
func run() -> int {
let list = List<int>()
list.Add(1)
list.Add(2)
list.Add(3)
return list.Count
}Runs `run` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Property_GetFromBclObject_StringLength topic: core status: verified
// verified behavior: Test.run(...) == 5
namespace Test
func run() -> int {
let s = "hello"
return s.Length
}Runs `go` → 25
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Protocol_AcrossFiles_DeclaredInA_ImplInB_UsedInC topic: core status: verified
// verified behavior: Test.go(...) == 25
namespace Test
interface IShape {
func area() -> int
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Protocol_EmitsInterfaceType topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
interface IDrawable {
func draw(x: int) -> string
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Protocol_VirtualDispatch_IL_Runtime topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
interface ISpeaker {
func speak() -> string
}
class Dog : ISpeaker {
name: string
}
func (d: Dog) speak() -> string {
return "woof"
}
func announce(s: ISpeaker) -> string {
return s.speak()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ProtocolType_ResolvesInMethodSignature topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
interface IWidget {
func render() -> string
}
func display(w: IWidget) -> int {
return 0
}Runs `test` → 2
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::RefData_ExprBodiedMethod topic: core status: verified
// verified behavior: Test.test(...) == 2
namespace Test
class Bag {
items: List<int>
init() {
self.items = List<int>()
}
func count() -> int = self.items.Count
func add(x: int) {
self.items.Add(x)
}
}
func test() -> int {
let b = Bag()
b.add(10)
b.add(20)
return b.count()
}Runs `run` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::RefDataMethods_BasicInstanceMethod topic: core status: verified
// verified behavior: Test.run(...) == 3
namespace Test
class Counter {
value: int
init() {
self.value = 0
}
func inc() {
self.value += 1
}
func get() -> int = self.value
}
func run() -> int {
let c = Counter()
c.inc()
c.inc()
c.inc()
return c.get()
}Runs `run` → "world"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::RefDataMethods_PubMethod topic: core status: verified
// verified behavior: Test.run(...) == "world"
namespace Test
class Greeter {
name: string
init(n: string) {
self.name = n
}
pub func greet() -> string = self.name
}
func run() -> string {
let g = Greeter("world")
return g.greet()
}