Skip to content

data — examples

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

Runs `scale` → 35

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

namespace Test

data Vec2 {
    x: int
    y: int
}

func scale(factor: int, v: Vec2) -> int {
    return v.x * factor + v.y * factor
}

Runs `run` → 2

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

namespace Test

ref data Bag {
    items: List<int>
    init() { self.items = List<int>() }
    func add(x: int) = self.items.Add(x)
    func count() -> int = self.items.Count
}

func run() -> int {
    let b = Bag()
    b.add(10)
    b.add(20)
    return b.count()
}

Runs `run` → 14

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

namespace Test

data Point {
    x: int
    y: int
}

func run() -> int {
    let p = Point { x: 3, y: 4 }
    let q = p with { x: 10 }
    return q.x + q.y
}

Runs `run` → 3

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

namespace Test

data Point {
    x: int
    y: int
}

func run() -> int {
    let p = Point { x: 3, y: 4 }
    let q = p with { x: 10 }
    return p.x
}

Runs `run` → 100

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

namespace Test

readonly data Vec {
    x: int
    y: int
}

func run() -> int {
    let v = Vec { x: 1, y: 2 }
    let w = v with { y: 99 }
    return w.x + w.y
}

Compiles

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

namespace Test

data Pt { x: int, y: int }

func go() -> int {
    let xs = List<Pt>()
    xs.Add(Pt { x: 10, y: 20 })
    xs.Add(Pt { x: 30, y: 40 })
    return xs[0].y + xs[1].x
}

Compiles

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

namespace Test

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

func go() -> int {
    let p = Pair<int, int> { first: 3, second: 7 }
    let q = Pair<int, int> { first: p.second, second: p.first }
    return q.first - q.second
}

Runs `go` → 9

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

namespace A

data Widget { a: int }

Rejected at compile time: ES2160

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

namespace Test

data widget { x: int }

Runs `go` → 42

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

namespace Lib

data Vec { x: int }

func bump(v: Vec) -> int {
    return v.x + 1
}

Runs `go` → 500

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

namespace Core

data Money { cents: int }

Runs `go` → 11

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

namespace Lib

data Vec { x: int }

func incr(n: int) -> int {
    return n + 1
}

Rejected at compile time: ES2142

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

namespace Test

data Vec { x: int }

func bump(v: Vec) -> int {
    return v.x + 1
}

func go() -> int {
    let v = Vec { x: 10 }
    return bump(v)
}

Compiles

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

namespace Test

data Widget { id: int }

func go() -> string {
    let w = Widget { id: 1 }
    return w.GetType().Name
}

Compiles

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

namespace Test
data Box { n: int }
func go() -> int {
    let xs = [new Box { n: 10 }, new Box { n: 20 }, new Box { n: 30 }]
    var t = 0
    for b in xs { t += b.n }
    return t
}

Compiles

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

namespace Test
data Box { n: int }
func go() -> int {
    let t = (Box { n: 2 }, Box { n: 5 })
    return t.Item2.n
}

Compiles

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

namespace Test
data Box { n: int }
func go() -> int {
    let xs = [Box { n: 10 }, Box { n: 20 }, Box { n: 30 }]
    var t = 0
    for b in xs { t += b.n }
    return t
}

Compiles

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

namespace Test
data Pt { x: int, y: int }
func go() -> int {
    let pts = List<Pt>()
    pts.Add(Pt { x: 10, y: 20 })
    let p = pts[0]
    return p.x + p.y
}

Compiles

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

namespace Test
data Acc { total: int }
func go() -> int {
    var a = new Acc { total: 0 }
    for i in 1..4 { a.total += i }
    return a.total
}

Compiles

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

namespace Test
data Box { n: int }
func go() -> int {
    let xs = [new Box { n: 1 }, new Box { n: 2 }, new Box { n: 3 }]
    var c = 0
    for b in xs { c += 1 }
    return c
}

Compiles

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

namespace Test
data Box { n: int }
func go() -> int {
    let pick = true
    let b = pick ? new Box { n: 2 } : new Box { n: 9 }
    return b.n
}

Compiles

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

namespace Test
data Acc { var total: int }
func go() -> int {
    var a = Acc { total: 0 }
    for i in 0..10 {
        a.total += i
    }
    return a.total
}

Compiles

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

namespace Test
data Point { x: int, y: int }
func go() -> int {
    let p = Point { x: 10, y: 20 }
    return p.x + p.y
}

Compiles

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

namespace Test
data Box { v: int }
func makeBox(n: int) -> Box {
    return Box { v: n * 2 }
}
func go() -> int {
    return makeBox(21).v
}

Compiles

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

namespace Test
data Sq { side: int }
func area(s: Sq) -> int {
    return s.side * s.side
}
func go() -> int {
    let s = Sq { side: 5 }
    return s.area()
}

Compiles

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

namespace Test
data Counter { var n: int }
func go() -> int {
    var c = Counter { n: 10 }
    c.n += 5
    return c.n
}

Compiles

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

namespace Test
data Vec2(x: int, y: int)
func go() -> int {
    let v = Vec2(3, 4)
    return v.x + v.y
}

Compiles

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

namespace Test
derive equality
data P { x: int, y: int }
func go() -> bool {
    let a = P { x: 1, y: 2 }
    let b = P { x: 1, y: 2 }
    return a == b
}

Compiles

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

namespace Test
derive equality
data P { x: int, y: int }
func go() -> bool {
    let a = P { x: 1, y: 2 }
    let b = P { x: 1, y: 9 }
    return a == b
}

Compiles

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

namespace Test
data Pair<A, B> { first: A, second: B }
func go() -> int {
    let p = Pair<int, int> { first: 3, second: 5 }
    return p.first + p.second
}

Compiles

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

namespace Test
data Pair<A, B> { first: A, second: B }
func go() -> int {
    let p = Pair<int, int> { first: 2, second: 1 }
    let q = Pair<int, int> { first: p.second, second: p.first }
    return q.first
}

Compiles

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

namespace Test
readonly data P { x: int, y: int }
func go() -> int {
    let a = P { x: 3, y: 4 }
    let b = a with { x: 9 }
    return b.x + b.y
}

Compiles

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

namespace Test
ref data Counter {
    var value: int
    init() { self.value = 0 }
    func inc() { self.value += 1 }
}
func go() -> int {
    let c = Counter()
    c.inc()
    c.inc()
    c.inc()
    return c.value
}

Compiles

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

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

Compiles

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

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)
}

Compiles

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

namespace Test
data Inner { v: int }
data Outer { inner: Inner, tag: int }
func go() -> int {
    let o = Outer { inner: Inner { v: 4 }, tag: 3 }
    return o.inner.v + o.tag
}

Runs `go` → (byte)128

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

namespace Test
data Pixel { r: byte, g: byte, b: byte }
func go() -> byte {
    let p = Pixel { r: 128, g: 64, b: 32 }
    return p.r
}

Compiles

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

namespace Test
data Vec { x: double, y: double }
func go() -> double {
    let v = Vec { x: 3.0, y: 4.0 }
    return v.x * v.x + v.y * v.y
}

Runs `go` → 30

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

namespace Test
data Vec { x: int, y: int }
func go() -> int {
    let v = Vec { x: 10, y: 20 }
    return v.x + v.y
}

Runs `go` → 5_000_000_000L

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

namespace Test
data Timestamp { epochMs: long }
func go() -> long {
    let t = Timestamp { epochMs: 5000000000 }
    return t.epochMs
}

Runs `go` → "Pair { first = a, second = b }"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::DeriveDebug_GenericStringFields   topic: data   status: verified
// verified behavior: Test.go(...) == "Pair { first = a, second = b }"

derive debug
data Pair<A, B> {
  first: A
  second: B
}
func go() -> string {
  let p = Pair<string, string> { first: "a", second: "b" }
  return p.ToString()
}

Runs `go` → "Pair { first = 3, second = 4 }"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::DeriveDebug_GenericToString   topic: data   status: verified
// verified behavior: Test.go(...) == "Pair { first = 3, second = 4 }"

derive debug
data Pair<A, B> {
  first: A
  second: B
}
func go() -> string {
  let p = Pair<int, int> { first: 3, second: 4 }
  return p.ToString()
}

Runs `go` → true

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

derive equality
data Point {
  x: int
  y: int
}
func go() -> bool {
  let a = Point { x: 1, y: 2 }
  let b = Point { x: 1, y: 2 }
  return a.Equals(b)
}

Runs `go` → 30

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

namespace Geometry

data Point {
    x: int
    y: int
}

func area(p: Point) -> int {
    return p.x * p.y
}

Runs `go` → 500

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

namespace Core

data Money {
    cents: int
}

Runs `go` → 7

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

namespace Geometry

data Point {
    x: int
    y: int
}

Compiles

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

namespace Test

pub ref data Program {
    var counter: int
    init() { self.counter = 0 }
    func run() -> int {
        for i in 1..5 {
            self.counter += i
        }
        return self.counter
    }
}

func main() -> int {
    let p = Program()
    return p.run()
}

Runs `go` → "Object"

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

namespace Test
pub ref data Widget {
    id: int
    init(id: int) { self.id = id }
}
func go() -> string {
    let w = Widget(1)
    return w.GetType().BaseType.Name
}

Runs `go` → "Widget"

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

namespace Test
pub ref data Widget {
    id: int
    init(id: int) { self.id = id }
}
func go() -> string {
    let w = Widget(7)
    return w.GetType().Name
}

Runs `go` → "Test"

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

namespace Test
pub ref data Widget {
    id: int
    init(id: int) { self.id = id }
}
func go() -> string {
    let w = Widget(1)
    return w.GetType().Namespace
}