Skip to content

pointers — examples

← all topics · 135 examples · page 1 of 3 · raw source ↓

Runs `line` → "inner-1"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis2_DeferLifoOrder_InsideAndAroundSpawn   topic: pointers   status: verified
// verified behavior: Test.line(...) == "inner-1"

namespace Test

data Log {
    var lines: List<string>
}

func run() -> int {
    var log: *Log = new Log { lines: List<string>() }
    let job = spawn {
        defer { log.lines.Add("inner-1") }
        defer { log.lines.Add("inner-2") }
        log.lines.Add("body")
    }
    defer { log.lines.Add("outer-1") }
    defer { log.lines.Add("outer-2") }
    job.Join()
    log.lines.Add("after-join")
    return log.lines.Count
}

func line(i: int) -> string {
    var log: *Log = new Log { lines: List<string>() }
    let job = spawn {
        defer { log.lines.Add("inner-1") }
        defer { log.lines.Add("inner-2") }
        log.lines.Add("body")
    }
    job.Join()
    return log.lines[i]
}

Runs `run` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis2_SpawnCapturesPointer_MutatesThroughIt   topic: pointers   status: verified
// verified behavior: Test.run(...) == 42

namespace Test

data Counter {
    var value: int
}

func run() -> int {
    var box: *Counter = new Counter { value: 0 }
    let job = spawn {
        box.value = box.value + 10
        box.value = box.value + 32
    }
    job.Join()
    return box.value
}

Runs `run` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: FeatureMixingTests.cs::Axis4_PointerEmbedded_PointerReceiverSatisfiesProtocol   topic: pointers   status: verified
// verified behavior: Test.run(...) == 42

namespace Test

data Inner {
    var n: int
}

func bump(i: *Inner) {
    i.n += 1
}

func value(i: Inner) -> int = i.n

data Outer : IBumper {
    *Inner
    label: string
}

interface IBumper {
    func bump()
    func value() -> int
}

func twice(b: IBumper) -> int {
    b.bump()
    b.bump()
    return b.value()
}

func run() -> int {
    var o: *Outer = new Outer { Inner: new Inner { n: 40 }, label: "x" }
    return twice(o)
}

Compiles

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

namespace Test
data Holder { p: *Cell }
data Cell { value: int }
func go() -> int {
    let h = Holder { p: new Cell { value: 42 } }
    return h.p.value
}

Compiles

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

namespace Test

data Vec { x: int }

func bump(v: *Vec) {
    v.x += 1
}

func go() -> int {
    var v: *Vec = new Vec { x: 10 }
    bump(v)
    return v.x
}

Compiles

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

namespace Test
data Box { n: int }
func go() -> int {
    var m = Dictionary<string, *Box>()
    m["k"] = new Box { n: 99 }
    return m["k"].n
}

Compiles

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

namespace Test
data Acc { var v: int }
func work(a: *Acc) {
    defer { a.v += 2 }
    defer { a.v *= 6 }
    a.v = 1
}
func go() -> int {
    // defers run LIFO after the function body: *6 then +2 → (1*6)+2 = 8.
    var a = new Acc { v: 0 }
    work(a)
    return a.v
}

Compiles

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

namespace Test
data Node { value: int, next: *Node }
func go() -> int {
    var head: *Node = nil
    if head == nil { return 0 }
    return head.value
}

Compiles

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

namespace Test
data Node { value: int, next: *Node }
func go() -> int {
    var head: *Node = new Node { value: 1, next: new Node { value: 2, next: nil } }
    var cur = head
    while cur != nil {
        if cur.value == 2 { return cur.value }
        cur = cur.next
    }
    return -1
}

Compiles

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

namespace Test
data Box { var n: int }
func go() -> int {
    var b: *Box = new Box { n: 2 }
    apply(b)
    return b.n
}
func apply(b: *Box) {
    defer { b.n += 2 }
    defer { b.n *= 5 }
}

Runs `go` → "inner-outer"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Coverage_ControlFlow.cs::Defer_NestedScopes   topic: pointers   status: verified
// verified behavior: Test.go(...) == "inner-outer"

namespace Test
data Buf { var s: string }
func go() -> string {
    var b: *Buf = new Buf { s: "" }
    outer(b)
    return b.s
}
func inner(b: *Buf) {
    defer { b.s = b.s + "inner-" }
}
func outer(b: *Buf) {
    defer { b.s = b.s + "outer" }
    inner(b)
}

Compiles

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

namespace Test
data Box { var n: int }
func go() -> int {
    var b: *Box = new Box { n: 0 }
    apply(b)
    return b.n
}
func apply(b: *Box) {
    defer { b.n *= 2 }
    for i in 1..5 {
        b.n += i
    }
}

Compiles

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

namespace Test
data Box { var n: int }
func go() -> int {
    var b: *Box = new Box { n: 0 }
    apply(b, true)
    return b.n
}
func apply(b: *Box, early: bool) {
    defer { b.n += 1 }
    if early { return }
    b.n += 100
}

Runs `go` → "321"

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

namespace Test
data Buf { var s: string }
func go() -> string {
    var b: *Buf = new Buf { s: "" }
    apply(b)
    return b.s
}
func apply(b: *Buf) {
    defer { b.s = b.s + "1" }
    defer { b.s = b.s + "2" }
    defer { b.s = b.s + "3" }
}

Compiles

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

namespace Test
data Box { var n: int }
func go() -> int {
    var b: *Box = new Box { n: 0 }
    bump(b)
    return b.n
}
func bump(b: *Box) {
    defer { b.n += 2 }
    defer { b.n *= 5 }
    b.n = 2
}

Compiles

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

namespace Test
data Box { var closed: int }
func go() -> int {
    var b: *Box = new Box { closed: 0 }
    work(b)
    return b.closed
}
func work(b: *Box) {
    defer { b.closed = 1 }
    let n = 5
}

Compiles

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

namespace Test
data Box { var closed: int }
func go() -> int {
    var b: *Box = new Box { closed: 0 }
    work(b, true)
    return b.closed
}
func work(b: *Box, bail: bool) {
    defer { b.closed = 1 }
    if bail { return }
    b.closed = 99
}

Compiles

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

namespace Test
data Node {
    value: int
    next: *Node
}
func go() -> int {
    var head: *Node = new Node { value: 1, next: nil }
    head = new Node { value: 2, next: head }
    head = new Node { value: 3, next: head }
    var len = 0
    var cur = head
    while cur != nil {
        len += 1
        cur = cur.next
    }
    return len
}

Runs `callViaPointer` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_FunctionPointers.cs::FunctionPointer_CallThroughLocal_ReturnsCorrectResult   topic: pointers   status: verified
// verified behavior: Test.callViaPointer(...) == 7

namespace Test

func add(a: int, b: int) -> int {
    return a + b
}

func callViaPointer() -> int {
    let ptr = &add
    return ptr(3, 4)
}

Runs `test` → 19

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_FunctionPointers.cs::FunctionPointer_InStructField_Works   topic: pointers   status: verified
// verified behavior: Test.test(...) == 19

namespace Test

func add(a: int, b: int) -> int {
    return a + b
}

func mul(a: int, b: int) -> int {
    return a * b
}

data BinOp {
    apply: &(int, int -> int)
}

func test() -> int {
    let adder = BinOp { apply: &add }
    let muler = BinOp { apply: &mul }
    return adder.apply(3, 4) + muler.apply(3, 4)
}

Runs `test` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_FunctionPointers.cs::FunctionPointer_TypedParameter_Works   topic: pointers   status: verified
// verified behavior: Test.test(...) == 42

namespace Test

func double(x: int) -> int {
    return x * 2
}

func apply(f: &(int -> int), value: int) -> int {
    return f(value)
}

func test() -> int {
    return apply(&double, 21)
}

Runs `test` → 25

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_FunctionPointers.cs::FunctionPointer_VoidReturn_Works   topic: pointers   status: verified
// verified behavior: Test.test(...) == 25

namespace Test

func double(x: int) -> int {
    return x * 2
}

func triple(x: int) -> int {
    return x * 3
}

func test() -> int {
    let d = &double
    let t = &triple
    return d(5) + t(5)
}

Runs `test` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_FunctionPointers.cs::FunctionPointer_WithByRefParam_Works   topic: pointers   status: verified
// verified behavior: Test.test(...) == 3

namespace Test

func increment(x: *int) {
    x += 1
}

func test() -> int {
    var count = 0
    let fn = &increment
    fn(*count)
    fn(*count)
    fn(*count)
    return count
}

Runs `test` → 30

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::Added_New_FieldSum   topic: pointers   status: verified
// verified behavior: Test.test(...) == 30

namespace Test

data Point { x: int, y: int }

func test() -> int {
    let p = new Point { x: 10, y: 20 }
    return p.x + p.y
}

Runs `test` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::Added_NilPointerDefaultThenAssign   topic: pointers   status: verified
// verified behavior: Test.test(...) == 42

namespace Test

data Node { value: int, next: *Node }

func test() -> int {
    var head: *Node = nil
    head = new Node { value: 42, next: nil }
    return head.value
}

Runs `test` → 5

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::Added_PointerFieldReassignedThroughChain   topic: pointers   status: verified
// verified behavior: Test.test(...) == 5

namespace Test

data Inner { var n: int }
data Outer { inner: *Inner }

func test() -> int {
    let o = new Outer { inner: new Inner { n: 1 } }
    o.inner.n = 5
    return o.inner.n
}

Runs `test` → 15

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::Added_PointerParameterMutatesCaller   topic: pointers   status: verified
// verified behavior: Test.test(...) == 15

namespace Test

data Counter { var value: int }

func bump(c: *Counter) { c.value += 10 }

func test() -> int {
    var c = new Counter { value: 5 }
    bump(c)
    return c.value
}

Runs `test` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::Added_PointerReturnedAndChained   topic: pointers   status: verified
// verified behavior: Test.test(...) == 42

namespace Test

data Box { n: int }

func make(v: int) -> *Box = new Box { n: v }

func test() -> int {
    let b = make(21)
    return b.n + b.n
}

Runs `test` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::Added_TwoLevelEmbedPointer   topic: pointers   status: verified
// verified behavior: Test.test(...) == 42

namespace Test

data Vec2 { var x: int, var y: int }
data Entity { *Vec2, name: string }

func test() -> int {
    var e = new Entity { Vec2: new Vec2 { x: 30, y: 12 }, name: "p" }
    return e.x + e.y
}

Runs `copyToHeap` → 77

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_AddressOfLocal_CopiesToHeap   topic: pointers   status: verified
// verified behavior: Test.copyToHeap(...) == 77

namespace Test

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

ref data Holder {
    pt: *Vec2
}

func copyToHeap() -> int {
    var h = Holder()
    let v = Vec2 { x: 77, y: 88 }
    h.pt = &v
    return h.pt.x
}

Runs `setThenNil` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_AssignNil   topic: pointers   status: verified
// verified behavior: Test.setThenNil(...) == true

namespace Test

data Inner { var x: int }

ref data Box {
    inner: *Inner
}

func setThenNil() -> bool {
    var b = Box()
    b.inner = new Inner { x: 10 }
    b.inner = nil
    return b.inner == nil
}

Runs `test` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_Auto_Deref_Field_Access   topic: pointers   status: verified
// verified behavior: Test.test(...) == 42

namespace Test

data Node { value: int }

func test() -> int {
    let n: *Node = new Node { value: 21 }
    return n.value * 2
}

Compiles

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

namespace Test

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

ref data Entity {
    pos: *Vec2
}

func readY() -> int {
    var e = Entity()
    e.pos = new Vec2 { x: 10, y: 20 }
    return e.pos.y
}

Compiles

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

namespace Test

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

ref data Entity {
    pos: *Vec2
}

func writeAndRead() -> int {
    var e = Entity()
    e.pos = new Vec2 { x: 0, y: 0 }
    e.pos.x = 99
    return e.pos.x
}

Runs `test` → 15

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_BothCallSyntaxes   topic: pointers   status: verified
// verified behavior: Test.test(...) == 15

namespace Test

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

func addX(v: *Vec2, amount: int) {
    v.x += amount
}

func test() -> int {
    var v: *Vec2 = new Vec2 { x: 0, y: 0 }
    addX(v, 10)
    v.addX(5)
    return v.x
}

Runs `increment` → 15

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_CompoundAssignment   topic: pointers   status: verified
// verified behavior: Test.increment(...) == 15

namespace Test

data Counter {
    var value: int
}

ref data Box {
    c: *Counter
}

func increment() -> int {
    var b = Box()
    b.c = new Counter { value: 10 }
    b.c.value += 5
    return b.c.value
}

Runs `test` → 70

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_DotSyntax_StaticCall   topic: pointers   status: verified
// verified behavior: Test.test(...) == 70

namespace Test

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

func scale(v: *Vec2, factor: int) {
    v.x *= factor
    v.y *= factor
}

func test() -> int {
    var v: *Vec2 = new Vec2 { x: 3, y: 4 }
    v.scale(10)
    return v.x + v.y
}

Compiles

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

namespace Test

data Inner {
    var x: int
}

ref data Outer {
    ptr: *Inner
}

func check() -> bool {
    var o = Outer()
    return o.ptr == nil
}

Runs `test` → 47

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_FieldInStruct   topic: pointers   status: verified
// verified behavior: Test.test(...) == 47

namespace Test

data Inner { var x: int }

data Outer {
    tag: int
    ptr: *Inner
}

func test() -> int {
    var o = Outer { tag: 5, ptr: new Inner { x: 42 } }
    return o.tag + o.ptr.x
}

Compiles

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

namespace Test

data Point {
    var x: int
    var y: int
}

ref data Holder {
    pt: *Point
}

func getX() -> int {
    var h = Holder()
    h.pt = new Point { x: 42, y: 7 }
    return h.pt.x
}

Runs `testSum` → 60

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_LinkedList_EndToEnd   topic: pointers   status: verified
// verified behavior: Test.testSum(...) == 60

namespace Test

data Node {
    value: int
    next: *Node
}

func prepend(head: *Node, value: int) -> *Node {
    return new Node { value: value, next: head }
}

func length(head: *Node) -> int {
    var count = 0
    var cur = head
    while cur != nil {
        count += 1
        cur = cur.next
    }
    return count
}

func testLength() -> int {
    var list: *Node = nil
    list = prepend(list, 10)
    list = prepend(list, 20)
    list = prepend(list, 30)
    return length(list)
}

func testSum() -> int {
    var list: *Node = nil
    list = prepend(list, 10)
    list = prepend(list, 20)
    list = prepend(list, 30)
    var total = 0
    var cur = list
    while cur != nil {
        total += cur.value
        cur = cur.next
    }
    return total
}

Runs `test` → 15

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_LocalVariable_ExplicitType   topic: pointers   status: verified
// verified behavior: Test.test(...) == 15

namespace Test

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

func test() -> int {
    var p: *Vec2 = new Vec2 { x: 5, y: 10 }
    return p.x + p.y
}

Runs `test` → 99

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_LocalVariable_NilThenAssign   topic: pointers   status: verified
// verified behavior: Test.test(...) == 99

namespace Test

data Inner { var x: int }

func test() -> int {
    var p: *Inner = nil
    if p == nil {
        p = new Inner { x: 99 }
    }
    return p.x
}

Runs `test` → 90

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_MultiplePointerFields   topic: pointers   status: verified
// verified behavior: Test.test(...) == 90

namespace Test

data Health {
    var current: int
    var max: int
}

data Position {
    var x: int
    var y: int
}

ref data Entity {
    hp: *Health
    pos: *Position
}

func test() -> int {
    var e = Entity()
    e.hp = new Health { current: 80, max: 100 }
    e.pos = new Position { x: 10, y: 20 }
    return e.hp.current + e.pos.x
}

Compiles

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

namespace Test

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

func addX(v: *Vec2, amount: int) {
    v.x += amount
}

func getSum() -> int {
    var v: *Vec2 = new Vec2 { x: 10, y: 20 }
    v.addX(5)
    return v.x
}

Runs `getValue` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_NamedReceiver_RefData   topic: pointers   status: verified
// verified behavior: Test.getValue(...) == 42

namespace Test

data Inner { var x: int }

ref data Manager {
    inner: *Inner
    var count: int
}

func setup(m: Manager) {
    m.inner = new Inner { x: 42 }
    m.count = 1
}

func getValue() -> int {
    var m = Manager()
    m.setup()
    return m.inner.x
}

Runs `test` → 77

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_NestedAutoDeref   topic: pointers   status: verified
// verified behavior: Test.test(...) == 77

namespace Test

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

data Transform {
    var position: Vec2
    var scale: double
}

ref data Entity {
    transform: *Transform
}

func test() -> int {
    var e = Entity()
    e.transform = new Transform {
        position: Vec2 { x: 33, y: 44 },
        scale: 1.0
    }
    return e.transform.position.x + e.transform.position.y
}

Runs `test` → 300

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_NestedAutoDeref_Write   topic: pointers   status: verified
// verified behavior: Test.test(...) == 300

namespace Test

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

data Transform {
    var position: Vec2
    var scale: double
}

ref data Entity {
    transform: *Transform
}

func test() -> int {
    var e = Entity()
    e.transform = new Transform {
        position: Vec2 { x: 0, y: 0 },
        scale: 1.0
    }
    e.transform.position.x = 100
    e.transform.position.y = 200
    return e.transform.position.x + e.transform.position.y
}

Runs `test` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_Nil_Default_Construction   topic: pointers   status: verified
// verified behavior: Test.test(...) == true

namespace Test

data Node { value: int }

func test() -> bool {
    let n: *Node = nil
    return n == nil
}

Runs `isNil` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_HeapPointer.cs::HeapPointer_NilByDefault   topic: pointers   status: verified
// verified behavior: Test.isNil(...) == true

namespace Test

data Inner { var x: int }

ref data Box {
    inner: *Inner
}

func isNil() -> bool {
    var b = Box()
    return b.inner == nil
}