struct — examples
← all topics · 202 examples · page 1 of 5 · raw source ↓
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DataContractTests.cs::ClassAttribute_ForcesClass topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
[Class]
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
readonly struct 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: struct status: verified
// verified behavior: reports diagnostic ES2002
namespace Test
struct 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: struct status: verified
// verified behavior: reports diagnostic ES2002
namespace Test
struct Tree {
value: int
children: List<Tree>
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DataContractTests.cs::SmallData_StaysStruct topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Point {
x: int
y: int
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DataContractTests.cs::StructAttribute_ForcesStruct topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
[Struct]
struct 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: DefaultNamedArgsTests.cs::CompositeDefault_NotStampedAsConstant topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Opts { depth: int = 0 }
func walk(n: int, o: Opts = Opts { depth: 1 }) -> int = n + o.depthRuns `go` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Default_CompositeLiteral_FreshPerCall topic: struct status: verified
// verified behavior: Test.go(...) == 0
namespace Test
struct Opts { depth: int = 0 }
func walk(o: Opts = Opts { depth: 0 }) -> int = o.depth
func go() -> int = walk()Runs `go` → 15
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Default_OnMethod topic: struct status: verified
// verified behavior: Test.go(...) == 15
namespace Test
struct Counter { v: int }
func (c: Counter) bump(by: int = 5) -> int = c.v + by
func go() -> int {
let c = Counter { v: 10 }
return c.bump()
}Runs `go` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DefaultNamedArgsTests.cs::Named_OnConstruction_PositionalData topic: struct status: verified
// verified behavior: Test.go(...) == 7
namespace Test
struct Vec2(x: int, y: int)
func go() -> int {
let v = Vec2(y: 4, x: 3)
return v.x + v.y
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: EmitterStressTests.cs::PositionalDataWithListLiterals topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
struct 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: struct status: verified
// verified behavior: Test.run(...) == 303
namespace Test
readonly struct 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: struct status: verified
// verified behavior: Test.run(...) == 34
namespace Test
struct Vec2 {
var x: int
var y: int
}
readonly struct 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: struct status: verified
// verified behavior: Test.run(...) == 33
namespace Test
struct Vec2 {
var x: int
var y: int
}
struct 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: struct status: verified
// verified behavior: Test.strPair(...) == false
namespace Test
derive equality
struct 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: struct status: verified
// verified behavior: Test.differingEmbedded(...) == false
namespace Test
struct Vec2 {
x: int
y: int
}
derive equality
struct 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::ValueData_TransformChain topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Vec { x: int, y: int }
func (v: Vec) add(o: Vec) -> Vec = Vec { x: v.x + o.x, y: v.y + o.y }
func (v: Vec) scaled(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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Transform {
Vec2
scale: int
}
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct A { b: B }
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct A { bs: List<B> }
struct 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
}Runs `go` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FuzzFindingsTests.cs::NestedDataField_ShadowedFieldName_ReadsInnerValue topic: struct status: verified
// verified behavior: Test.go(...) == 7
namespace Test
struct Inner { v: int }
struct Outer { v: int, inner: Inner }
func (o: Outer) readInner() -> int {
return o.inner.v
}
func go() -> int {
return Outer { v: 100, inner: Inner { v: 7 } }.readInner()
}FuzzFindingsTests__PromotedMethod_NestedFieldInListLiteral_ResolvesAgainstTargetType
struct runnable verifiedRuns `go` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FuzzFindingsTests.cs::PromotedMethod_NestedFieldInListLiteral_ResolvesAgainstTargetType topic: struct status: verified
// verified behavior: Test.go(...) == 5
namespace Test
struct D0 { f0: int, f1: int }
struct D1 { f0: int, f1: int, f2: D0 }
func (p1_0: D1) h1() -> int {
let v1 = [p1_0.f2.f0, (-27)]
return p1_0.f0
}
func h2(p2_0: int) -> int {
return D1 { f0: p2_0, f1: (-4), f2: D0 { f0: p2_0, f1: 39 } }.h1()
}
func go() -> int {
return h2(5)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Attribute_ObsoleteWithMessage_EmittedOntoType topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
[Obsolete("do not use")]
struct Legacy {
value: int
}
func run() -> int { return 0 }Runs `run` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::Default_StructZeroed topic: struct status: verified
// verified behavior: Test.run(...) == 0
namespace Test
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
derive debug
struct Marker {
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::DeriveDebug_Struct_ToStringContainsFieldValues topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
derive debug
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
derive equality
struct Point {
x: int
y: int
}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: struct status: verified
// verified behavior: Test.go(...) == false
namespace Test
derive equality
struct 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: struct status: verified
// verified behavior: Test.go(...) == true
namespace Test
derive equality
struct 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: struct status: verified
// verified behavior: Test.go(...) == true
namespace Test
derive equality
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
derive equality, debug
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace T
struct Config(name: string, url: string)
class 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 `go` → "hello"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::GenericData_Construction_MixedTypes topic: struct status: verified
// verified behavior: Test.go(...) == "hello"
namespace Test
struct 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: struct status: verified
// verified behavior: Test.makePair(...) == 7
namespace Test
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct 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: struct status: verified
// verified behavior: Test.strPair(...) == "b"
namespace Test
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Item {
name: string
value: int
}
struct Bag {
items: List<Item>
}Runs `make` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::LetField_IsEmittedAsInitOnly topic: struct status: verified
// verified behavior: Test.make(...) == 3
namespace Test
struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct 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: struct status: verified
// verified behavior: Test.makeAndSum(...) == 7
namespace Test
struct Vec2(x: int, y: int)
func (v: Vec2) sum() -> 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: struct status: verified
// verified behavior: Test.make(...) == 42
namespace Test
struct Item(name: string, value: int)
func make() -> int {
let a = Item("hello", 42)
return a.value
}Runs `make` → 7.0f
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::ReadonlyData_AllFieldsInitOnly topic: struct status: verified
// verified behavior: Test.make(...) == 7.0f
namespace Test
readonly struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
readonly struct Pt {
x: int
y: int
}
func make() -> int { return 0 }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: struct status: verified
// verified behavior: Test.scale(...) == 35
namespace Test
struct Vec2 {
x: int
y: int
}
func scale(factor: int, v: Vec2) -> int {
return v.x * factor + v.y * factor
}Runs `run` → 14
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::With_CopyAndOverwrite_ProducesNewValue topic: struct status: verified
// verified behavior: Test.run(...) == 14
namespace Test
struct 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: struct status: verified
// verified behavior: Test.run(...) == 3
namespace Test
struct 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: struct status: verified
// verified behavior: Test.run(...) == 100
namespace Test
readonly struct 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: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct 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: 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: 7 }
let q = Pair<int, int> { first: p.second, second: p.first }
return q.first - q.second
}