Skip to content

data — examples

← all topics · 199 examples · page 3 of 4 · raw source ↓

Runs `go` → "Point"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::Reflection_ValueDataTypeName   topic: data   status: verified
// verified behavior: Test.go(...) == "Point"

namespace Test
data Point { x: int, y: int }
func go() -> string {
    let p = Point { x: 1, y: 2 }
    return p.GetType().Name
}

Runs `go` → 15

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Nominal.cs::Added_InterfaceMethodViaPromotion   topic: data   status: verified
// verified behavior: Test.go(...) == 15

namespace Test
interface IArea { func area() -> int }
data Rect : IArea { w: int, h: int }
func area(r: Rect) -> int = r.w * r.h
func go() -> int { let r = Rect { w: 3, h: 5 } let a: IArea = r return a.area() }

Runs `go` → 8

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Nominal.cs::Added_InterfaceReturnedFromFunction   topic: data   status: verified
// verified behavior: Test.go(...) == 8

namespace Test
interface ISized { func size() -> int }
ref data Crate : ISized {
    n: int
    init(n: int) { self.n = n }
    func size() -> int = self.n
}
func make() -> ISized = Crate(8)
func go() -> int { let s = make() return s.size() }

Runs `go` → 5

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Nominal.cs::Added_MarkerInterface_NoMethods_Conforms   topic: data   status: verified
// verified behavior: Test.go(...) == 5

namespace Test
interface ITag { }
data Item : ITag { v: int }
func tagged(t: ITag) -> int = 5
func go() -> int { let i = Item { v: 1 } return tagged(i) }

Runs `go` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Nominal.cs::Added_RefData_DeclaresInterface_Works   topic: data   status: verified
// verified behavior: Test.go(...) == 7

namespace Test
interface ICounter { func get() -> int }
ref data Counter : ICounter {
    value: int
    init(v: int) { self.value = v }
    func get() -> int = self.value
}
func read(c: ICounter) -> int = c.get()
func go() -> int { let c = Counter(7) return read(c) }

Runs `go` → 30

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

namespace Test
interface INamed { func label() -> int }
interface ISized { func size() -> int }
data Widget : INamed, ISized { a: int, b: int }
func label(w: Widget) -> int = w.a
func size(w: Widget) -> int = w.b
func both(n: INamed, s: ISized) -> int = n.label() + s.size()
func go() -> int { let w = Widget { a: 10, b: 20 } return both(w, w) }

Rejected at compile time: ES2153

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Nominal.cs::Added_UndeclaredStructuralMatch_WarnsES2153   topic: data   status: verified
// verified behavior: reports diagnostic ES2153

namespace Test
interface ISized { func size() -> int }
data Crate { items: int }
func size(c: Crate) -> int = c.items

Runs `go` → 11

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

namespace Test
interface ISized { func size() -> int }
data Crate : ISized { items: int }
func size(c: Crate) -> int { return c.items + 1 }
func measure(s: ISized) -> int { return s.size() }
func go() -> int { let c = Crate { items: 10 } return measure(c) }

Rejected at compile time: ES2153

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Nominal.cs::Undeclared_StructuralMatch_RejectedAndSuggested   topic: data   status: verified
// verified behavior: reports diagnostic ES2153

namespace Test
interface ISized { func size() -> int }
data Crate { items: int }
func size(c: Crate) -> int { return c.items + 1 }
func measure(s: ISized) -> int { return s.size() }
func go() -> int { let c = Crate { items: 10 } return measure(c) }

Runs `go` → 15

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_PtrEmbedIface.cs::ValueEmbed_PromotedFieldAccess   topic: data   status: verified
// verified behavior: Test.go(...) == 15

data Vec2 {
  var x: int
  var y: int
}

Compiles

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

namespace Test

ref data Q {
    n: int

    init(n: int) { self.n = n }

    func touch() { } // no -> Type, no returns clause: void
}

func go() {
    let q = Q(0)
    q.touch()
}

Compiles

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

namespace Test

ref data Q {
    returns Q
    n: int

    init(n: int) { self.n = n }

    func bump() { return Q(self.n + 1) }
}

Runs `go` → 1

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

namespace Test

ref data Q {
    returns Q
    n: int

    init(n: int) { self.n = n }

    func bump() { return Q(self.n + 1) }
    func count() -> int { return self.n }
}

func go() -> int {
    let q = Q(0)
    return q.bump().count()
}

Runs `go` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_On_RefData_Fluent_Chaining   topic: data   status: verified
// verified behavior: Test.go(...) == 3

namespace Test

ref data Q {
    returns Q
    n: int

    init(n: int) { self.n = n }

    func bump() { return Q(self.n + 1) }
}

func go() -> int {
    let q = Q(0)
    let r = q.bump().bump().bump()
    return r.n
}

Runs `go` → 1

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

namespace Test

ref data Q {
    returns Q
    n: int

    init(n: int) { self.n = n }

    func bump() { return Q(self.n + 1) }
}

func go() -> int {
    let q = Q(0)
    let r = q.bump()
    return r.n
}

Runs `go` → 2

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Returns.cs::Returns_Clause_Sees_Cross_Method_Resolution_With_DataType   topic: data   status: verified
// verified behavior: Test.go(...) == 2

namespace Test

ref data Counter {
    returns Counter
    n: int

    init(n: int) { self.n = n }

    func inc() { return Counter(self.n + 1) }
    func twice() { return self.inc().inc() }
}

func go() -> int { return Counter(0).twice().n }

Runs `go` → 10

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

namespace Test

ref data Box {
    n: int

    init(n: int) { self.n = n }

    func twice() returns int { return self.n * 2 }
}

func go() -> int { return Box(5).twice() }

Runs `run` → 75

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Bank_Account_With_Data_Type_Pin   topic: data   status: verified
// verified behavior: Test.run(...) == 75

namespace Test

data Account { balance: int }

func deposit(a: Account, amount: int) -> Account = a with { balance: a.balance + amount }
func withdraw(a: Account, amount: int) -> Account = a with { balance: a.balance - amount }

func run() -> int {
    let opened = Account { balance: 0 }
    let after_deposit = opened.deposit(100)
    let after_withdraw = after_deposit.withdraw(30)
    let final = after_withdraw.deposit(5)
    return final.balance
}

Runs `test` → 111

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Composite_Nested_Literal   topic: data   status: verified
// verified behavior: Test.test(...) == 111

namespace Test

data Outer { x: int, inner: Inner }
data Inner { a: int, b: int }

func test() -> int {
    let o = Outer { x: 1, inner: Inner { a: 10, b: 100 } }
    return o.x + o.inner.a + o.inner.b
}

Runs `test` → 103

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Composite_Partial_With_Rebinds_One_Field   topic: data   status: verified
// verified behavior: Test.test(...) == 103

namespace Test

data Point { x: int, y: int, z: int }

func test() -> int {
    let p = Point { x: 1, y: 2, z: 3 }
    let q = p with { y: 99 }
    return q.x + q.y + q.z
}

Runs `test` → 321

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Data_Method_Chain_Via_With_Pin   topic: data   status: verified
// verified behavior: Test.test(...) == 321

namespace Test

data Vec3 { x: int, y: int, z: int }

func with_x(v: Vec3, nx: int) -> Vec3 = v with { x: nx }
func with_y(v: Vec3, ny: int) -> Vec3 = v with { y: ny }
func with_z(v: Vec3, nz: int) -> Vec3 = v with { z: nz }

func test() -> int {
    let origin = Vec3 { x: 0, y: 0, z: 0 }
    let stepped = origin.with_x(1).with_y(2).with_z(3)
    return stepped.x + stepped.y * 10 + stepped.z * 100
}

Runs `test` → 30

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Function_Returns_Data_Used_By_Caller   topic: data   status: verified
// verified behavior: Test.test(...) == 30

namespace Test

data Pair { left: int, right: int }

func make(a: int, b: int) -> Pair = Pair { left: a, right: b }

func test() -> int {
    let p = make(10, 20)
    return p.left + p.right
}

Runs `test` → 73

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Interface_Dispatch_Through_Promoted_Method_Pin   topic: data   status: verified
// verified behavior: Test.test(...) == 73

namespace Test

interface IShape {
    func area() -> int
}

data Square : IShape { side: int }
data Circle : IShape { radius: int }

func area(s: Square) -> int = s.side * s.side
func area(c: Circle) -> int = 3 * c.radius * c.radius

func test() -> int {
    let sq = Square { side: 5 }
    let ci = Circle { radius: 4 }
    return sq.area() + ci.area()
}

Runs `test` → 99

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Nested_Data_Field_Access_Two_Deep   topic: data   status: verified
// verified behavior: Test.test(...) == 99

namespace Test

data Inner { v: int }
data Outer { inner: Inner }

func test() -> int {
    let o = Outer { inner: Inner { v: 99 } }
    return o.inner.v
}

Runs `describe` → 38

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Promoted_Instance_Methods_On_Data_Pin   topic: data   status: verified
// verified behavior: Test.describe(...) == 38

namespace Test

data Rect { width: int, height: int }

func area(r: Rect) -> int = r.width * r.height
func perimeter(r: Rect) -> int = (r.width + r.height) * 2
func is_square(r: Rect) -> bool = r.width == r.height

func describe() -> int {
    let r = Rect { width: 4, height: 5 }
    var score = 0
    if r.is_square() {
        score = score + 1000
    }
    return r.area() + r.perimeter() + score
}

Runs `test` → false

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::RefData_Different_Instances_Not_Identity_Equal_Pin   topic: data   status: verified
// verified behavior: Test.test(...) == false

namespace Test

ref data Box {
    pub value: int
    init(v: int) { self.value = v }
}

func test() -> bool {
    let a = Box(5)
    let b = Box(5)
    return a == b
}

Runs `test` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::RefData_Identity_Equal_To_Self_Pin   topic: data   status: verified
// verified behavior: Test.test(...) == true

namespace Test

ref data Box {
    pub value: int
    init(v: int) { self.value = v }
}

func test() -> bool {
    let a = Box(5)
    let b = a
    return a == b
}

Runs `test` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Triple_Nested_Data_Field_Access   topic: data   status: verified
// verified behavior: Test.test(...) == 42

namespace Test

data A { v: int }
data B { a: A }
data C { b: B }

func test() -> int {
    let c = C { b: B { a: A { v: 42 } } }
    return c.b.a.v
}

Runs `test` → "alice:30"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Data_Composite_With_String_Field   topic: data   status: verified
// verified behavior: Test.test(...) == "alice:30"

namespace Test

data User { name: string, age: int }

func test() -> string {
    let u = User { name: "alice", age: 30 }
    return u.name + ":" + u.age.ToString()
}

Runs `test` → 1

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Data_Equality_Field_By_Field   topic: data   status: verified
// verified behavior: Test.test(...) == 1

namespace Test

data P { x: int, y: int }

func test() -> int {
    let a = P { x: 1, y: 2 }
    let b = P { x: 1, y: 2 }
    if a.x == b.x && a.y == b.y {
        return 1
    }
    return 0
}

Runs `test` → 12

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Data_Field_Read_After_Local_Assign   topic: data   status: verified
// verified behavior: Test.test(...) == 12

namespace Test

data P { x: int, y: int }

func test() -> int {
    var p = P { x: 0, y: 0 }
    p.x = 5
    p.y = 7
    return p.x + p.y
}

Runs `test` → 21

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Data_Multiple_Construction_Sites   topic: data   status: verified
// verified behavior: Test.test(...) == 21

namespace Test

data Coord { x: int, y: int }

func test() -> int {
    let a = Coord { x: 1, y: 2 }
    let b = Coord { x: 3, y: 4 }
    let c = Coord { x: 5, y: 6 }
    return a.x + b.x + c.x + a.y + b.y + c.y
}

Runs `test` → 99

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Data_Single_Field_Composite   topic: data   status: verified
// verified behavior: Test.test(...) == 99

namespace Test

data Wrap { v: int }

func test() -> int {
    let w = Wrap { v: 99 }
    return w.v
}

Runs `test` → 0

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Data_With_All_Default_Fields_Compiles   topic: data   status: verified
// verified behavior: Test.test(...) == 0

namespace Test

data Empty { }

func test() -> int {
    let e = Empty { }
    return 0
}

Runs `test` → 28

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Data_With_Preserves_Other_Fields   topic: data   status: verified
// verified behavior: Test.test(...) == 28

namespace Test

data Cfg { a: int, b: int, c: int, d: int }

func test() -> int {
    let c = Cfg { a: 1, b: 2, c: 3, d: 4 }
    let c2 = c with { b: 20 }
    return c2.a + c2.b + c2.c + c2.d
}

Runs `test` → 1004

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Data_With_Single_Field_Update   topic: data   status: verified
// verified behavior: Test.test(...) == 1004

namespace Test

data P { x: int, y: int }

func test() -> int {
    let p = P { x: 3, y: 4 }
    let q = p with { x: 10 }
    return q.x * 100 + q.y
}

Runs `test` → 15

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Data_With_Used_In_Return   topic: data   status: verified
// verified behavior: Test.test(...) == 15

namespace Test

data P { x: int, y: int }

func translate(p: P, dx: int) -> P = p with { x: p.x + dx }

func test() -> int {
    let start = P { x: 0, y: 5 }
    let moved = start.translate(10)
    return moved.x + moved.y
}

Runs `test` → 100

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Embedding_Direct_Field_Access_Pin   topic: data   status: verified
// verified behavior: Test.test(...) == 100

namespace Test

data Inner { value: int }

data Outer {
    pub Inner
}

func test() -> int {
    let o = Outer { Inner: Inner { value: 100 } }
    return o.Inner.value
}

Runs `test` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Function_Returns_RefData_Pin   topic: data   status: verified
// verified behavior: Test.test(...) == 7

namespace Test

ref data Card {
    pub face: int
    init(f: int) { self.face = f }
}

func make(n: int) -> Card = Card(n)

func test() -> int {
    let c = make(7)
    return c.face
}

Runs `test` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Generic_Pair_Construction   topic: data   status: verified
// verified behavior: Test.test(...) == 7

namespace Test

data Pair<A, B> { first: A, second: B }

func test() -> int {
    let p = Pair<int, int> { first: 3, second: 4 }
    return p.first + p.second
}

Runs `test` → "x7"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Generic_Pair_String_Int   topic: data   status: verified
// verified behavior: Test.test(...) == "x7"

namespace Test

data Pair<A, B> { first: A, second: B }

func test() -> string {
    let p = Pair<string, int> { first: "x", second: 7 }
    return p.first + p.second.ToString()
}

Runs `test` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Interface_Method_Resolution_On_Data   topic: data   status: verified
// verified behavior: Test.test(...) == 42

namespace Test

interface IGet {
    func get() -> int
}

data Box : IGet { v: int }

func get(b: Box) -> int = b.v

func test() -> int {
    let b = Box { v: 42 }
    return b.get()
}

Runs `test` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Interface_Method_Returns_Bool   topic: data   status: verified
// verified behavior: Test.test(...) == true

namespace Test

interface ITest {
    func passes() -> bool
}

data Suite : ITest {
    flag: bool
}

func passes(s: Suite) -> bool = s.flag

func test() -> bool {
    let s = Suite { flag: true }
    return s.passes()
}

Runs `test` → 30

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Interface_Polymorphic_Param   topic: data   status: verified
// verified behavior: Test.test(...) == 30

namespace Test

interface IGet {
    func get() -> int
}

data A : IGet { x: int }
data B : IGet { y: int }

func get(a: A) -> int = a.x
func get(b: B) -> int = b.y

func consume(g: IGet) -> int = g.get()

func test() -> int {
    let a = A { x: 10 }
    let b = B { y: 20 }
    return consume(a) + consume(b)
}

Runs `test` → 15

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::RefData_Field_Mutation_Persists   topic: data   status: verified
// verified behavior: Test.test(...) == 15

namespace Test

ref data Bag {
    var capacity: int
    init(c: int) { self.capacity = c }
}

func test() -> int {
    let b = Bag(0)
    b.capacity = 10
    b.capacity = b.capacity + 5
    return b.capacity
}

Runs `test` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::RefData_Init_Stores_Param_To_Field   topic: data   status: verified
// verified behavior: Test.test(...) == 42

namespace Test

ref data Box {
    pub n: int
    init(value: int) { self.n = value }
}

func test() -> int {
    let b = Box(42)
    return b.n
}

Runs `test` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::RefData_Instance_Method_Accesses_Self_Field   topic: data   status: verified
// verified behavior: Test.test(...) == 3

namespace Test

ref data Counter {
    var n: int
    init() { self.n = 0 }

    func bump() {
        self.n = self.n + 1
    }

    func value() -> int = self.n
}

func test() -> int {
    let c = Counter()
    c.bump()
    c.bump()
    c.bump()
    return c.value()
}

Runs `test` → 321

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::RefData_Multi_Field_Construction   topic: data   status: verified
// verified behavior: Test.test(...) == 321

namespace Test

ref data Point3 {
    pub x: int
    pub y: int
    pub z: int
    init(a: int, b: int, c: int) {
        self.x = a
        self.y = b
        self.z = c
    }
}

func test() -> int {
    let p = Point3(1, 2, 3)
    return p.x + p.y * 10 + p.z * 100
}

Rejected at compile time: ES2145

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: IndexDiagnosticTests.cs::IndexingData_Errors   topic: data   status: verified
// verified behavior: reports diagnostic ES2145

namespace Test

data P { x: int }

func f(p: P) -> int { return p[0] }

Runs `null` → "greeter"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::CSharp_Interface_Implemented_By_Esharp_Data   topic: data   status: verified
// verified behavior: Test.null(...) == "greeter"

namespace Test

data Greeter : IDescribable {}

func describe(g: Greeter) -> string = "greeter"