Skip to content

allocation — examples

← all topics · 72 examples · page 2 of 2 · raw source ↓

Runs `go` → 30

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

namespace Test
data Inner { n: int }
data Outer { inner: *Inner, label: string }
func go() -> int {
    let o = new Outer { inner: new Inner { n: 30 }, label: "x" }
    return o.inner.n
}

ILEmitterTests_New__New_NestedThreeLevels

allocation runnable verified

Runs `go` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_NestedThreeLevels   topic: allocation   status: verified
// verified behavior: Test.go(...) == 7

namespace Test
data A { n: int }
data B { a: *A }
data C { b: *B }
func go() -> int {
    let c = new C { b: new B { a: new A { n: 7 } } }
    return c.b.a.n
}

ILEmitterTests_New__New_NotNil

allocation runnable verified

Runs `go` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_NotNil   topic: allocation   status: verified
// verified behavior: Test.go(...) == true

namespace Test
data Box { n: int }
func go() -> bool {
    let b = new Box { n: 1 }
    return b != nil
}

ILEmitterTests_New__New_PointerComparedEqual

allocation runnable verified

Runs `go` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_PointerComparedEqual   topic: allocation   status: verified
// verified behavior: Test.go(...) == true

namespace Test
data Box { n: int }
func go() -> bool {
    let a = new Box { n: 1 }
    let b = a
    return a == b
}

Runs `go` → 15

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

namespace Test
data Vec2 { var x: int, var y: int }
data Entity { *Vec2, name: string }
func go() -> int {
    var e = new Entity { Vec2: new Vec2 { x: 10, y: 5 }, name: "p" }
    e.x += 0
    return e.x + e.y
}

Runs `go` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_PointerSatisfiesInterface   topic: allocation   status: verified
// verified behavior: Test.go(...) == 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 go() -> int {
    var o: *Outer = new Outer { Inner: new Inner { n: 40 }, label: "x" }
    return twice(o)
}

Runs `go` → 20

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

namespace Test
data Vec2(x: int, y: int)
func go() -> int {
    let a = new Vec2(3, 4)
    let b = new Vec2 { x: 6, y: 7 }
    return a.x + a.y + b.x + b.y
}

ILEmitterTests_New__New_Positional_ExprArgs

allocation runnable verified

Runs `go` → 13

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_Positional_ExprArgs   topic: allocation   status: verified
// verified behavior: Test.go(...) == 13

namespace Test
data Vec2(x: int, y: int)
func go() -> int {
    let a = 5
    let v = new Vec2(a + 1, a + 2)
    return v.x + v.y
}

ILEmitterTests_New__New_Positional_Mutation

allocation runnable verified

Runs `go` → 15

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

namespace Test
data Vec2(x: int, y: int)
func go() -> int {
    var v = new Vec2(3, 4)
    v.x = 11
    return v.x + v.y
}

ILEmitterTests_New__New_Positional_OneArg

allocation runnable verified

Runs `go` → 42

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

namespace Test
data Wrap(v: int)
func go() -> int {
    let w = new Wrap(42)
    return w.v
}

ILEmitterTests_New__New_Positional_TwoArgs

allocation runnable verified

Runs `go` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_Positional_TwoArgs   topic: allocation   status: verified
// verified behavior: Test.go(...) == 7

namespace Test
data Vec2(x: int, y: int)
func go() -> int {
    let v = new Vec2(3, 4)
    return v.x + v.y
}

Rejected at compile time: ES2144

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_PositionalGenericExternal_IsError_List   topic: allocation   status: verified
// verified behavior: reports diagnostic ES2144

namespace Test
func go() -> int {
    let d = new Dictionary<string, int>()
    return 0
}

ILEmitterTests_New__New_ReadonlyData

allocation runnable verified

Runs `go` → 5

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_ReadonlyData   topic: allocation   status: verified
// verified behavior: Test.go(...) == 5

namespace Test
readonly data Point { x: int, y: int }
func go() -> int {
    let p = new Point { x: 2, y: 3 }
    return p.x + p.y
}

ILEmitterTests_New__New_ReturnedFromBranch

allocation runnable verified

Runs `go` → 9

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

namespace Test
data Box { n: int }
func make(big: bool) -> *Box {
    if big { return new Box { n: 9 } }
    return new Box { n: 1 }
}
func go() -> int { let b = make(true) return b.n }

Runs `go` → "n=5"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_StringInterpolationViaLocal   topic: allocation   status: verified
// verified behavior: Test.go(...) == "n=5"

namespace Test
data Box { n: int }
func go() -> string {
    let b = new Box { n: 5 }
    return "n={b.n}"
}

Runs `go` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_TwoInstances_IndependentIdentity   topic: allocation   status: verified
// verified behavior: Test.go(...) == 3

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

ILEmitterTests_New__New_TwoPointersNotEqual

allocation runnable verified

Runs `go` → false

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_TwoPointersNotEqual   topic: allocation   status: verified
// verified behavior: Test.go(...) == false

namespace Test
data Box { n: int }
func go() -> bool {
    let a = new Box { n: 1 }
    let b = new Box { n: 1 }
    return a == b
}

ILEmitterTests_New__New_Var_Reassign

allocation runnable verified

Runs `go` → 2

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_Var_Reassign   topic: allocation   status: verified
// verified behavior: Test.go(...) == 2

namespace Test
data Box { n: int }
func go() -> int {
    var b = new Box { n: 1 }
    b = new Box { n: 2 }
    return b.n
}

Compiles

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

namespace Test
data Node { value: int, next: *Node }
func go() -> int {
    var n2: *Node = SPELL Node { value: 2, next: nil }
    var n1: *Node = SPELL Node { value: 1, next: n2 }
    return n1.value + n1.next.value
}

Compiles

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

namespace Test
data Node { value: int, next: *Node }
func go() -> int {
    let n = SPELL Node { value: 7, next: nil }
    return n.value
}

Rejected at compile time: ES2143

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_EmitsNoDeprecationWarning   topic: allocation   status: unverified
// verified behavior: reports diagnostic ES2143

namespace Test
data Box { n: int }
func go() -> int {
    let b = new Box { n: 7 }
    return b.n
}

Runs `go` → 13

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_New.cs::New_PassedToGenericFunction   topic: allocation   status: unverified
// verified behavior: Test.go(...) == 13

namespace Test
data Box { n: int }
func identity<T>(v: T) -> T = v
func go() -> int { let b = identity(new Box { n: 13 }) return b.n }