Skip to content

data — examples

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

Compiles

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

namespace Test

[HeapAlloc]
data Point {
    x: int
    y: int
}

Compiles

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

namespace Test

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

Rejected at compile time: ES2002

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

namespace Test

data Node {
    value: int
    next: Node
}

Rejected at compile time: ES2002

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

namespace Test

data Tree {
    value: int
    children: List<Tree>
}

DataContractTests__SmallData_StaysStruct

data unknown verified ×5

Compiles

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

namespace Test

data Point {
    x: int
    y: int
}

Compiles

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

namespace Test

[StackAlloc]
data 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: EmitterStressTests.cs::PositionalDataWithListLiterals   topic: data   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace T

data Config(name: string, url: string, category: string)

func defaults() -> List<Config> {
    return [
        Config("A", "http://a.com", "cat1"),
        Config("B", "http://b.com", "cat2"),
    ]
}

Runs `run` → 303

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

namespace Test

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

func run() -> int {
    let p = Pair<int, int> { first: 1, second: 2 }
    let q = p with { first: 100 }
    let r = q with { second: 200 }
    return r.first + r.second + p.first + p.second
}

Runs `run` → 34

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

namespace Test

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

readonly data Transform {
    Vec2
    scale: int
}

func run() -> int {
    let t = Transform { x: 1, y: 2, scale: 3 }
    let u = t with { Vec2: Vec2 { x: 10, y: 20 } }
    return u.x + u.y + u.scale + t.x
}

Runs `run` → 33

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

namespace Test

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

data Wrap {
    Vec2
    tag: int
}

func run() -> int {
    let w = Wrap { x: 1, y: 2, tag: 3 }
    let q = w with { Vec2: Vec2 { x: 10, y: 20 } }
    return q.x + q.y + q.tag
}

Runs `strPair` → false

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

namespace Test

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

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

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

Runs `differingEmbedded` → false

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

namespace Test

data Vec2 {
    x: int
    y: int
}

derive equality
data Box {
    Vec2
    tag: int
}

func sameTag() -> bool {
    let a = Box { x: 1, y: 2, tag: 7 }
    let b = Box { x: 1, y: 2, tag: 7 }
    return a.Equals(b)
}

func differingEmbedded() -> bool {
    let a = Box { x: 1, y: 2, tag: 7 }
    let b = Box { x: 99, y: 2, tag: 7 }
    return a.Equals(b)
}

Compiles

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

namespace Test
ref data Acc {
    var n: int
    init() { self.n = 0 }
}
func add(a: Acc, 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: data   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
ref data Acc {
    var n: int
    init() { self.n = 0 }
}
func add(a: Acc, 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: data   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
ref data Acc {
    var n: int
    init() { self.n = 0 }
}
func add(a: Acc, 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::ValueData_TransformChain   topic: data   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
data Vec { x: int, y: int }
func add(v: Vec, o: Vec) -> Vec = Vec { x: v.x + o.x, y: v.y + o.y }
func scaled(v: Vec, k: int) -> Vec = Vec { x: v.x * k, y: v.y * k }
func go() -> int {
    let r = (Vec { x: 3, y: 2 }).add(Vec { x: 1, y: 4 }).scaled(4)
    return r.x + r.y    // (4,6)*4 = (16,24) → 40
}

Compiles

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

namespace Test
data Transform {
    Vec2
    scale: int
}
data Vec2 {
    var x: int
    var y: int
}
func go() -> int {
    var t = Transform { x: 10, y: 20, scale: 5 }
    t.x += 5
    return t.x + t.y + t.scale   // 15 + 20 + 5
}

Compiles

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

namespace Test
data A { b: B }
data B { n: int }
func go() -> int {
    let a = A { b: B { n: 7 } }
    return a.b.n
}

Compiles

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

namespace Test
data A { bs: List<B> }
data B { n: int }
func go() -> int {
    let xs = List<B>()
    xs.Add(B { n: 1 })
    xs.Add(B { n: 2 })
    let a = A { bs: xs }
    return a.bs.Count
}

Compiles

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

namespace Test

[Obsolete("do not use")]
data Legacy {
    value: int
}

func run() -> int { return 0 }

Compiles

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

namespace Test

[Serializable]
ref data Config {
    name: string
}

ILEmitterTests__Default_StructZeroed

data runnable verified

Runs `run` → 0

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

namespace Test

data P {
    x: int
    y: int
}

func run() -> int {
    let p = default(P)
    return p.x + p.y
}

Compiles

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

namespace Test

derive debug
data Marker {
}

Compiles

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

namespace Test

derive debug
data Point {
    x: int
    y: int
}

Compiles

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

namespace Test

derive equality
data Point {
    x: int
    y: int
}

Compiles

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

namespace Test

derive equality
ref data Config {
    name: string
    port: int

    init(name: string, port: int) {
        self.name = name
        self.port = port
    }
}

Runs `go` → false

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

namespace Test

derive equality
data Point {
    x: int
    y: int
}

func go() -> bool {
    let p1 = Point { x: 3, y: 4 }
    let p2 = Point { x: 3, y: 5 }
    return p1.Equals(p2)
}

Runs `go` → true

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

namespace Test

derive equality
data Unit {
}

func go() -> bool {
    let a = Unit {}
    let b = Unit {}
    return a.Equals(b)
}

Runs `go` → true

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

namespace Test

derive equality
data Point {
    x: int
    y: int
}

func go() -> bool {
    let p1 = Point { x: 3, y: 4 }
    let p2 = Point { x: 3, y: 4 }
    return p1.Equals(p2)
}

Compiles

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

namespace Test

derive equality, debug
data Pair {
    a: int
    b: string
}

Compiles

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

namespace T

data Config(name: string, url: string)

ref data Aggregator {
    configs: List<Config>
    count: int
    init() {
        self.configs = List<Config>()
        self.count = 0
    }
    func getCount() -> int = self.count
    func addConfig(name: string, url: string) = self.configs.Add(Config(name, url))
}

Runs `run` → 21

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

namespace Test

ref data 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 `go` → "hello"

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

namespace Test

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

func go() -> string {
    let p = Pair<int, string> { first: 42, second: "hello" }
    return p.second
}

Runs `makePair` → 7

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

namespace Test

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

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

Compiles

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

namespace Test

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

Runs `strPair` → "b"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericData_TwoInstantiations_AreDistinctAtRuntime   topic: data   status: verified
// verified behavior: Test.strPair(...) == "b"

namespace Test

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

func intPair() -> int {
    let p = Pair<int, int> { first: 10, second: 20 }
    return p.first
}

func strPair() -> string {
    let p = Pair<string, string> { first: "a", second: "b" }
    return p.second
}

Compiles

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

namespace Test

data Container {
    items: List<int>
}

Compiles

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

namespace Test

data Item {
    name: string
    value: int
}

data Bag {
    items: List<Item>
}

Compiles

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

namespace Test

ref data 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 `make` → 3

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

namespace Test

data Point {
    let x: int
    var y: int
}

func make() -> int {
    let p = Point { x: 1, y: 2 }
    return p.x + p.y
}

Compiles

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

namespace Test

data Thing {
    a: int
}

Runs `makeAndSum` → 7

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

namespace Test

data Vec2(x: int, y: int)

func sum(v: Vec2) -> int {
    return v.x + v.y
}

func makeAndSum() -> int {
    let v = Vec2(3, 4)
    return v.sum()
}

Runs `make` → 42

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

namespace Test

data Item(name: string, value: int)

func make() -> int {
    let a = Item("hello", 42)
    return a.value
}

Runs `make` → "hello"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::PositionalData_RefData   topic: data   status: verified
// verified behavior: Test.make(...) == "hello"

namespace Test

ref data Label(text: string, size: int)

func getText(l: Label) -> string {
    return l.text
}

func make() -> string {
    let l = Label("hello", 12)
    return l.getText()
}

Compiles

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

namespace Test

interface ISpeaker {
    func speak() -> string
}

ref data Dog : ISpeaker {
    name: string
}

func speak(d: Dog) -> string {
    return "woof"
}

func announce(s: ISpeaker) -> string {
    return s.speak()
}

Runs `make` → 7.0f

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ReadonlyData_AllFieldsInitOnly   topic: data   status: verified
// verified behavior: Test.make(...) == 7.0f

namespace Test

readonly data Vec2 {
    x: float
    y: float
}

func make() -> float {
    let v = Vec2 { x: 3.0, y: 4.0 }
    return v.x + v.y
}

Compiles

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

namespace Test

readonly data Pt {
    x: int
    y: int
}

func make() -> 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: data   status: verified
// verified behavior: Test.test(...) == 2

namespace Test

ref data 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: data   status: verified
// verified behavior: Test.run(...) == 3

namespace Test

ref data 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: data   status: verified
// verified behavior: Test.run(...) == "world"

namespace Test

ref data 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()
}