Skip to content

struct — examples

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

Compiles

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

namespace Test
struct Point { x: int, y: int }
func origin() -> Point {
    let p = Point { x: 0, y: 0 }
    return p
}

Compiles

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

namespace Test
struct Widget { id: int }
func make() -> Widget = Widget { id: 1 }
func use() -> int {
    let w = make()
    return w.id
}

Compiles

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

namespace A
struct Widget { var v: int }

Compiles

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

namespace Test
struct Box { var v: int }
func (b: Box) tenfold() -> int = b.v * 10
func go() -> int {
    let p = Box { v: 5 }
    return p.tenfold()
}

Runs `gap1` → 50

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SymbolSpineTests.cs::PointerMethods_ValueReceiverMethod_CallableOnPointer   topic: struct   status: verified
// verified behavior: Test.gap1(...) == 50


namespace Test
struct Box { var v: int }
func (b: Box) tenfold() -> int = b.v * 10
func gap1() -> int {
    let p = new Box { v: 5 }
    return p.tenfold()
}

Runs `go` → 42

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

namespace Test
struct Vec { x: int }
func (v: Vec) bump() -> int { return v.x + 1 }
func go() -> int {
    let v = Vec { x: 41 }
    let s = v.ToString()
    return v.bump()
}
func run() -> int { return go() }

Rejected at compile time: ES2147

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TotalBinderTests.cs::UnknownMemberOnData_IsLocatedBinderError   topic: struct   status: verified
// verified behavior: reports diagnostic ES2147

struct Point { x: int, y: int }
func (p: Point) main() -> int {
    return p.zz
}

Compiles

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

namespace Lines

struct Vec2 {
    x: float
    y: float
}

struct Line {
    a: Vec2
    b: Vec2
}

Compiles

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

namespace Big

struct Big {
    a: decimal
    b: int
}

Compiles

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

namespace Wrap

struct Wrapper {
    name: string
}

Compiles

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

namespace Geo

struct Point {
    x: int
    y: int
}

Compiles

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

namespace Types

struct Counter {
    value: int
}

Compiles

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

namespace Proto

interface IDescribable {
    func describe() -> string
}

struct Widget : IDescribable {
    label: string
}

func (w: Widget) describe() -> string {
    return w.label
}

Compiles

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

namespace Types

struct Point {
    x: int
    y: int
}

Compiles

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

namespace IO

struct FileHandle : IDisposable {
    path: string
}

func (h: FileHandle) Dispose() {
    Console.WriteLine("closed")
}

Compiles

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

namespace Dbg

derive debug
struct Color {
    r: int
    g: int
    b: int
}

Compiles

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

namespace Eq

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

Compiles

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

namespace Gen

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

func makePair<A, B>(a: A, b: B) -> Pair<A, B> {
    return Pair<A, B> { first: a, second: b }
}

Compiles

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

namespace Test

struct User {
    name: string
}

func find(id: int) -> User? {
    return nil
}

Compiles

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

namespace Test

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

struct Widget {
    Base
    label: string
}

func getX() -> int {
    var w = Widget { x: 10, y: 20, label: "test" }
    return w.x
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TypeGrammarTests.cs::GenericField_SubstitutesClosedArgs   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<string, int> { first: "abc", second: 5 }
    return p.first.Length + p.second
}

Compiles

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

namespace Test
struct Pair<A, B> { first: A, second: B }
func (p: Pair<A, B>) swapped<A, B>() -> Pair<B, A> = Pair<B, A> { first: p.second, second: p.first }

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

Runs `go` → 11

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

namespace A
struct Widget { size: int }

Compiles

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

namespace Test
struct Point { x: int, y: float }

Rejected at compile time: ES3012

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: CtorOverloadTests.cs::Data_InitBlock_StillES3012   topic: struct   status: unverified
// verified behavior: reports diagnostic ES3012

namespace Test

struct Vec {
    x: int
    init(x: int) { self.x = x }
}

Compiles

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

namespace Test

[Class]
class Conn {
    host: string
    port: int
}

Rejected at compile time: ES3012

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

namespace Test

struct Point {
    x: int
    y: int

    init(px: int, py: int) {
        self.x = px
        self.y = py
    }
}

Rejected at compile time: ES3012

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

namespace Test

class Point {
    x: int
    y: int

    init(px: int, py: int) {
        self.x = px
        self.y = py
    }
}

Rejected at compile time: ES3012

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

namespace Test

struct Vec2(x: int, y: int)

func main() {
    let v = Vec2(3, 4)
}

Rejected at compile time: ES2002

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

namespace Test

struct Tree {
    value: int
    children: List<*Tree>
}

Rejected at compile time: ES2002

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: DataContractTests.cs::RecursiveField_PointerForm_Ok   topic: struct   status: unverified
// 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_RefDataAllowsSelfReference   topic: struct   status: unverified
// verified behavior: reports diagnostic ES2002

namespace Test

class Node {
    value: int
    next: Node
}

Compiles

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

namespace Test

[Struct]
[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::StructAttribute_OnClass_Errors   topic: struct   status: unverified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

[Struct]
class Conn {
    host: string
    port: int
}

Compiles

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

namespace T

struct Item(a: string, b: string, c: string, d: DateTimeOffset, e: string, f: string)

func run(synd: object, feed: object) {
    var items = List<Item>()
    for entry in synd.Items {
        let link = entry.Links.Count > 0 ? entry.Links[0].Uri.ToString() : ""
        let summary = entry.Summary?.Text ?? ""
        let pub = entry.PublishDate != DateTimeOffset.MinValue ? entry.PublishDate : entry.LastUpdatedTime
        items.Add(Item(entry.Title.Text, link, feed.name, pub, summary, feed.category))
    }
}

Compiles

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

namespace Test

struct Point {
    let x: int
    y: int
}

func bad() -> int {
    var p = Point { x: 1, y: 2 }
    p.x = 99
    return p.x
}

Rejected at compile time: ES2001

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::StructPromotion_LargeStruct_StaysStruct_AutopromotionDisabled   topic: struct   status: unverified
// verified behavior: reports diagnostic ES2001

namespace Test

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

Rejected at compile time: ES2001

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::StructPromotion_ManyRefFields_StaysStruct_AutopromotionDisabled   topic: struct   status: unverified
// verified behavior: reports diagnostic ES2001

namespace Test

struct Refs {
    a: string
    b: string
    c: string
    d: int
}

Rejected at compile time: ES2001

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests.cs::StructPromotion_StoredInCollection_StaysStruct_AutopromotionDisabled   topic: struct   status: unverified
// verified behavior: reports diagnostic ES2001

namespace Test

struct Item {
    a: double
    b: double
    c: double
    d: double
    e: double
}

struct Container {
    items: List<Item>
}

Rejected at compile time: ES2150

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

namespace Lib

struct Vec { x: int }

Compiles

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

namespace Test
interface IAdder { func add(x: int) -> int }
struct Calc : IAdder { base: int }
func (c: Calc) add(x: string) -> int = c.base

Compiles

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

namespace Test
interface ISized { func size() -> int }
struct Crate { items: int }
func (c: Crate) size() -> int = c.items
func measure(s: ISized) -> int = s.size()
func go() -> int { let c = Crate { items: 3 } return measure(c) }

Compiles

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

namespace Test
interface IAdder { func add(n: int) -> int }
struct Box : IAdder { v: int }
func (b: Box) add(n: string) -> int { return b.v }

Compiles

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

namespace Test
interface ISized { func size() -> int }
struct Crate : ISized { items: int }
func (c: Crate) size() -> string { return "x" }

Compiles

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

namespace Test

struct Circle { r: float }

readonly func (c: Circle) bad() { c.r = 1.0 }

Compiles

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

namespace Test
class Select {
    struct Arm { kind: int, body: int }
    arms: int
    init() { self.arms = 0 }
}
func make() -> Select = Select()

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: RequiredDeconstructTests.cs::Deconstruct_ArityMismatch_Errors   topic: struct   status: unverified
// 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 (a, b, c) = v
    return a
}

Compiles

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

struct Point { x: int, y: int }

func (p: Point) broken() -> int {
    return p.x +
}

func (p: Point) fine() -> int {
    return p.x + p.y
}

Compiles

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

namespace Test

interface IWidget {
    func render() -> string
}

struct SmallWidget {
    label: string
}

func (w: SmallWidget) render() -> string {
    return w.label
}

func test() -> string {
    let w: IWidget = SmallWidget { label: "hi" }
    return w.render()
}

Compiles

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

namespace T
interface IShape { func area() -> int }
struct Circle : IShape {
    r: int
    func area() -> int { return 3 * this.r }
}