struct — examples
← all topics · 202 examples · page 3 of 5 · raw source ↓
Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Receivers.cs::InlineMethod_Minimal_Struct topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Foo { x: int func Bar() -> int = self.x }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Receivers.cs::ReadonlyReceiver_Reads topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Circle { r: float }
readonly func (c: Circle) diameter() -> float = c.r * 2.0
func run() -> float {
let c = Circle { r: 5.0 }
return c.diameter()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Receivers.cs::ValueReceiver_IsSnapshot_MutationDoesNotWriteBack topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Circle { r: float }
func (c: Circle) grow() { c.r = 99.0 }
func run() -> float {
var c = Circle { r: 2.0 }
c.grow()
return c.r
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Receivers.cs::ValueReceiver_ReadsField topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Circle { r: float }
func (c: Circle) area() -> float = c.r * c.r * 3.14159
func run() -> float {
let c = Circle { r: 2.0 }
return c.area()
}Runs `go` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::E02_ReturnValueTypeSelf_ByValue topic: struct status: verified
// verified behavior: Test.go(...) == 5
namespace Test
struct Counter { n: int }
func (c: Counter) same() -> Counter = c
func go() -> int {
let c = Counter { n: 5 }
return c.same().n
}Runs `go` → 12
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::H09_TwoGenericArities_Coexist topic: struct status: verified
// verified behavior: Test.go(...) == 12
namespace Test
struct Pair<A> { a: A }
struct Pair<A, B> { a: A, b: B }
func go() -> int {
let one = Pair<int> { a: 5 }
let two = Pair<int, int> { a: 3, b: 4 }
return one.a + two.a + two.b
}Runs `go` → 12
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::J12_Arity_TwoGenericArities topic: struct status: verified
// verified behavior: Test.go(...) == 12
namespace Test
struct Cell<A> { a: A }
struct Cell<A, B> { a: A, b: B }
func go() -> int {
let one = Cell<int> { a: 5 }
let two = Cell<int, int> { a: 3, b: 4 }
return one.a + two.a + two.b
}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: struct status: verified
// verified behavior: Test.run(...) == 75
namespace Test
struct Account { balance: int }
func (a: Account) deposit(amount: int) -> Account = a with { balance: a.balance + amount }
func (a: Account) withdraw(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: struct status: verified
// verified behavior: Test.test(...) == 111
namespace Test
struct Outer { x: int, inner: Inner }
struct 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: struct status: verified
// verified behavior: Test.test(...) == 103
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 321
namespace Test
struct Vec3 { x: int, y: int, z: int }
func (v: Vec3) with_x(nx: int) -> Vec3 = v with { x: nx }
func (v: Vec3) with_y(ny: int) -> Vec3 = v with { y: ny }
func (v: Vec3) with_z(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: struct status: verified
// verified behavior: Test.test(...) == 30
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 73
namespace Test
interface IShape {
func area() -> int
}
struct Square : IShape { side: int }
struct Circle : IShape { radius: int }
func (s: Square) area() -> int = s.side * s.side
func (c: Circle) area() -> 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: struct status: verified
// verified behavior: Test.test(...) == 99
namespace Test
struct Inner { v: int }
struct 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: struct status: verified
// verified behavior: Test.describe(...) == 38
namespace Test
struct Rect { width: int, height: int }
func (r: Rect) area() -> int = r.width * r.height
func (r: Rect) perimeter() -> int = (r.width + r.height) * 2
func (r: Rect) is_square() -> 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` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Triple_Nested_Data_Field_Access topic: struct status: verified
// verified behavior: Test.test(...) == 42
namespace Test
struct A { v: int }
struct B { a: A }
struct 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: struct status: verified
// verified behavior: Test.test(...) == "alice:30"
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 1
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 12
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 21
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 99
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 0
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 28
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 1004
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 15
namespace Test
struct P { x: int, y: int }
func (p: P) translate(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: struct status: verified
// verified behavior: Test.test(...) == 100
namespace Test
struct Inner { value: int }
struct 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::Generic_Pair_Construction topic: struct status: verified
// verified behavior: Test.test(...) == 7
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == "x7"
namespace Test
struct 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: struct status: verified
// verified behavior: Test.test(...) == 42
namespace Test
interface IGet {
func get() -> int
}
struct Box : IGet { v: int }
func (b: Box) get() -> 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: struct status: verified
// verified behavior: Test.test(...) == true
namespace Test
interface ITest {
func passes() -> bool
}
struct Suite : ITest {
flag: bool
}
func (s: Suite) passes() -> 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: struct status: verified
// verified behavior: Test.test(...) == 30
namespace Test
interface IGet {
func get() -> int
}
struct A : IGet { x: int }
struct B : IGet { y: int }
func (a: A) get() -> int = a.x
func (b: B) get() -> 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)
}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: struct status: verified
// verified behavior: reports diagnostic ES2145
namespace Test
struct P { x: int }
func (p: P) f() -> 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: struct status: verified
// verified behavior: Test.null(...) == "greeter"
namespace Test
struct Greeter : IDescribable {}
func (g: Greeter) describe() -> string = "greeter"Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::Esharp_Data_Type_Used_From_Csharp topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
pub struct Point { x: int, y: int }Runs `go` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MultiLineParamsTests.cs::PositionalData_Header_MultiLine topic: struct status: verified
// verified behavior: Test.go(...) == 7
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: PrimaryCtorCaptureTests.cs::Data_PositionalForm_Unchanged topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Vec2(x: int, y: int)
func (v: Vec2) sum() -> int { return v.x + v.y }
func run() -> int {
let v = Vec2(3, 4)
return v.sum()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Deconstruct_GenericPositionalData topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Pair<A, B>(first: A, second: B)
func run() -> int {
let p = Pair<int, int>(20, 3)
let (a, b) = p
return a + b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Deconstruct_InvocableViaReflection topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Vec2(x: int, y: int)
func make() -> Vec2 {
return Vec2(8, 9)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Deconstruct_LetDestructure_OverPositionalData topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Vec2(x: int, y: int)
func run() -> int {
let v = Vec2(3, 4)
let (x, y) = v
return x * 10 + y
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Deconstruct_MethodEmittedWithOutParams topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Vec2(x: int, y: int)
func touch() -> int {
let v = Vec2(1, 2)
return v.x
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Deconstruct_MixedTypes topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Entry(name: string, score: int)
func run() -> string {
let e = Entry("kae", 9)
let (n, s) = e
return n + s.ToString()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Deconstruct_NotEmittedOnBodyFormData topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Plain {
a: int
}
func touch() -> int {
let p = Plain { a: 1 }
return p.a
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Required_AllSupplied_Compiles topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Span {
required lo: int
required hi: int
}
func run() -> int {
let s = Span { lo: 2, hi: 9 }
return s.hi - s.lo
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Required_EmitsRequiredMemberAttribute topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Span {
required lo: int
hi: int
}
func touch() -> int {
let s = Span { lo: 1 }
return s.lo
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Required_NonRequiredFieldsStillDefaultSilently topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Cfg {
required port: int
retries: int
}
func run() -> int {
let c = Cfg { port: 80 }
return c.port + c.retries
}Rejected at compile time: ES2189
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Required_Omitted_ES2189 topic: struct status: verified
// verified behavior: reports diagnostic ES2189
namespace Test
struct Span {
required lo: int
required hi: int
}
func run() -> int {
let s = Span { lo: 2 }
return s.lo
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SemanticModelTests.cs::DeclarationsIn_EnumeratesFileDeclarationsInOrder topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Point { x: int, y: int }
func (p: Point) dist() -> int = p.x + p.y
const LIMIT = 10Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SemanticModelTests.cs::Describe_RendersHoverLines topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Point { x: int, y: int }
func (p: Point) dist() -> int = p.x + p.yCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SemanticModelTests.cs::FindReferences_Field_RoundTrips topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Box { v: int }
func (b: Box) read() -> int = b.v + b.vCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SemanticModelTests.cs::GetSymbolAt_TypeDeclarationAndUse_SameSymbol topic: struct status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Point { x: int, y: int }
func origin() -> Point = Point { x: 0, y: 0 }