Skip to content

struct — examples

← all topics · 202 examples · page 2 of 5 · raw source ↓

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: struct   status: verified
// verified behavior: Test.go(...) == 9

namespace A

struct 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: struct   status: verified
// verified behavior: reports diagnostic ES2160

namespace Test

struct widget { x: int }

Runs `go` → 11

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

namespace Lib

struct Vec { x: int }

func (v: Vec) bump() -> 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: struct   status: verified
// verified behavior: Test.go(...) == 500

namespace Core

struct 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: struct   status: verified
// verified behavior: Test.go(...) == 11

namespace Lib

struct 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: struct   status: verified
// verified behavior: reports diagnostic ES2142

namespace Test

struct Vec { x: int }

func (v: Vec) bump() -> 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct Sq { side: int }
func (s: Sq) area() -> 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
derive equality
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
derive equality
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
readonly struct 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::StructEmbedding_PromotedAccess   topic: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct Vec2 {
    var x: int
    var y: int
}
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
interface ISized {
    func size() -> int
}
struct Crate : ISized {
    items: int
}
func (c: Crate) size() -> 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct Inner { v: int }
struct 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: struct   status: verified
// verified behavior: Test.go(...) == (byte)128

namespace Test
struct 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: struct   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
struct 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: struct   status: verified
// verified behavior: Test.go(...) == 30

namespace Test
struct 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: struct   status: verified
// verified behavior: Test.go(...) == 5_000_000_000L

namespace Test
struct 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: struct   status: verified
// verified behavior: Test.go(...) == "Pair { first = a, second = b }"

derive debug
struct 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: struct   status: verified
// verified behavior: Test.go(...) == "Pair { first = 3, second = 4 }"

derive debug
struct 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: struct   status: verified
// verified behavior: Test.go(...) == true

derive equality
struct 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)
}

Compiles

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

namespace Geo

struct Rect {
    w: int
    h: int
}

Runs `go` → 30

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

namespace Geometry

struct Point {
    x: int
    y: int
}

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

Runs `go` → 20

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

namespace Geo

struct Rect {
    w: int
    h: int
}

func (r: Rect) area() -> int {
    return r.w * r.h
}

Runs `go` → 500

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

namespace Core

struct 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: struct   status: verified
// verified behavior: Test.go(...) == 7

namespace Geometry

struct Point {
    x: int
    y: int
}

Runs `go` → "Point"

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

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

Runs `go` → 9

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

namespace Test
interface ISized { func size() -> int }
struct Pair<A> : ISized { a: A, count: int }
func (p: Pair<A>) size<A>() -> int = p.count
func go() -> int { let p = Pair<int> { a: 1, count: 9 } let s: ISized = p return s.size() }

Runs `go` → 15

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

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

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: struct   status: verified
// verified behavior: Test.go(...) == 5

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

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: struct   status: verified
// verified behavior: Test.go(...) == 30

namespace Test
interface INamed { func label() -> int }
interface ISized { func size() -> int }
struct Widget : INamed, ISized { a: int, b: int }
func (w: Widget) label() -> int = w.a
func (w: Widget) size() -> 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: struct   status: verified
// verified behavior: reports diagnostic ES2153

namespace Test
interface ISized { func size() -> int }
struct Crate { items: int }
func (c: Crate) size() -> 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: struct   status: verified
// verified behavior: Test.go(...) == 11

namespace Test
interface ISized { func size() -> int }
struct Crate : ISized { items: int }
func (c: Crate) size() -> 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: struct   status: verified
// verified behavior: reports diagnostic ES2153

namespace Test
interface ISized { func size() -> int }
struct Crate { items: int }
func (c: Crate) size() -> 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: struct   status: verified
// verified behavior: Test.go(...) == 15

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

Compiles

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

namespace Test

struct Circle { r: float }

func area(c: Circle) -> float = c.r * c.r

func run() -> float {
    let c = Circle { r: 3.0 }
    return area(c)
}