Skip to content

inheritance — examples

← all topics · 40 examples · page 1 of 1 · raw source ↓

Compiles

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

namespace Test
ref data Dog : Animal {
    extra: int
    init(e: int) : base(4) { self.extra = e }
}
open ref data Animal {
    legs: int
    init(l: int) { self.legs = l }
}
func go() -> int {
    let d = Dog(3)
    return d.legs + d.extra
}

Compiles

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

namespace Test
abstract ref data Shape {
    init() { }
    abstract func area() -> int
}
ref data Square : Shape {
    side: int
    init(s: int) : base() { self.side = s }
    : func area() -> int { return self.side * self.side }
}
func go() -> int {
    let sq = Square(5)
    return sq.area()
}

Compiles

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

namespace Test

abstract ref data Shape {
    init() { }
    abstract func area() -> int
}

Compiles

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

namespace Test

abstract ref data Shape {
    init() { }
}

Compiles

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

namespace Test
abstract ref data Shape {
    init() { }
    abstract func area() -> int
}

Runs `go` → "dog"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Added_BaseCall_PassesArgs   topic: inheritance   status: verified
// verified behavior: Test.go(...) == "dog"

namespace Test
open ref data Animal {
    species: string
    init(s: string) { self.species = s }
}
ref data Dog : Animal {
    name: string
    init(n: string) : base("dog") { self.name = n }
}
func go() -> string {
    let d = Dog("rex")
    return d.species
}

Runs `go` → 42

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

namespace Test
open ref data Base {
    n: int
    init(n: int) { self.n = n }
}
ref data Derived : Base {
    init() : base(42) { }
}
func go() -> int {
    let d = Derived()
    return d.n
}

Runs `go` → 4

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Added_VirtualNotOverridden_UsesBase   topic: inheritance   status: verified
// verified behavior: Test.go(...) == 4

namespace Test
open ref data Animal {
    init() { }
    virtual func legs() -> int { return 4 }
}
ref data Snake : Animal {
    init() : base() { }
}
func go() -> int {
    // Snake doesn't override legs(): callvirt resolves to Animal's base body.
    var a: Animal = Snake()
    return a.legs()
}

Runs `go` → 2

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

namespace Test
open ref data Animal {
    init() { }
    virtual func sound() -> int { return 1 }
}
ref data Dog : Animal {
    init() : base() { }
    : func sound() -> int { return 2 }
}
func go() -> int {
    // Base-typed local holding a derived instance — callvirt dispatches to Dog.
    var a: Animal = Dog()
    return a.sound()
}

Runs `test` → 42

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

namespace Test

open ref data Base {
    pub n: int
    init(n: int) { self.n = n }
}

ref data Sub : Base {
    init(n: int) : base(n) {}
}

func test() -> int {
    let s = Sub(42)
    return s.n
}

Runs `go` → 16

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Colon_Func_Fulfills_Abstract_Parent   topic: inheritance   status: verified
// verified behavior: Test.go(...) == 16

namespace Test

abstract ref data Shape {
    init() { }
    abstract func area() -> int
}

ref data Square : Shape {
    side: int
    init(s: int) : base() { self.side = s }
    : func area() -> int { return self.side * self.side }
}

func go() -> int {
    let s = Square(4)
    return s.area()
}

Compiles

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

namespace Test

open ref data Animal {
    init() { }
    virtual func speak() -> string = "..."
}

ref data Dog : Animal {
    init() : base() { }
    : func speak() -> string = "woof"
}

Runs `go` → "woof"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Colon_Func_Overrides_Virtual_Parent   topic: inheritance   status: verified
// verified behavior: Test.go(...) == "woof"

namespace Test

open ref data Animal {
    init() { }
    virtual func speak() -> string = "..."
}

ref data Dog : Animal {
    init() : base() { }
    : func speak() -> string = "woof"
}

func go() -> string {
    let d = Dog()
    return d.speak()
}

Rejected at compile time: ES2122

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Colon_Func_With_No_Matching_Parent_Is_ES2122   topic: inheritance   status: verified
// verified behavior: reports diagnostic ES2122

namespace Test

open ref data Animal {
    init() { }
}

ref data Dog : Animal {
    init() : base() { }
    : func bark() -> string = "woof"
}

Rejected at compile time: ES2121

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Colon_Func_Without_Body_In_Concrete_Subclass_Is_ES2121   topic: inheritance   status: verified
// verified behavior: reports diagnostic ES2121

namespace Test

abstract ref data Shape {
    init() { }
    abstract func area() -> int
}

ref data Square : Shape {
    init() : base() { }
    : func area() -> int
}

Runs `go` → "dog"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Init_Calls_BaseCtor_With_Args   topic: inheritance   status: verified
// verified behavior: Test.go(...) == "dog"

namespace Test

open ref data Animal {
    species: string
    init(s: string) { self.species = s }
}

ref data Dog : Animal {
    init() : base("dog") { }
}

func go() -> string {
    let d = Dog()
    return d.species
}

Rejected at compile time: ES2128

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Init_Mismatched_BaseArgs_Is_ES2128   topic: inheritance   status: verified
// verified behavior: reports diagnostic ES2128

namespace Test

open ref data Animal {
    name: string
    init(n: string) { self.name = n }
}

ref data Dog : Animal {
    init() : base() { }
}

Compiles

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

namespace Test

open ref data Animal {
    init() {}
}

Compiles

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

namespace Test

open ref data Animal {
    init() { }
}

Rejected at compile time: ES2120

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Plain_Func_Shadowing_Virtual_Parent_Is_ES2120   topic: inheritance   status: verified
// verified behavior: reports diagnostic ES2120

namespace Test

open ref data Animal {
    init() { }
    virtual func speak() -> string = "..."
}

ref data Dog : Animal {
    init() : base() { }
    func speak() -> string = "woof"
}

Runs `goDog` → "woof"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Polymorphic_Dispatch_Through_Base_Class_Reference   topic: inheritance   status: verified
// verified behavior: Test.goDog(...) == "woof"

namespace Test

open ref data Animal {
    init() { }
    virtual func speak() -> string = "..."
}

ref data Cat : Animal {
    init() : base() { }
    : func speak() -> string = "meow"
}

ref data Dog : Animal {
    init() : base() { }
    : func speak() -> string = "woof"
}

func goCat() -> string {
    let c = Cat()
    return c.speak()
}
func goDog() -> string {
    let d = Dog()
    return d.speak()
}

Compiles

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

namespace Test

ref data Locked {
    init() {}
}

Compiles

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

namespace Test

ref data Dog {
    init() { }
}

Compiles

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

namespace Test

sealed ref data Cat {
    init() { }
}

Compiles

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

namespace Test

abstract ref data Shape {
    init() { }
}

ref data Square : Shape {
    init() : base() { }
}

Compiles

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

namespace Test

open ref data Animal {
    init() { }
}

ref data Dog : Animal {
    init() : base() { }
}

Compiles

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

namespace Test

ref data Cat {
    init() { }
}

ref data Bob : Cat {
    init() { }
}

Compiles

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

namespace Test

interface INamed { func name() -> string }

open ref data Animal {
    init() { }
}

ref data Dog : Animal, INamed {
    init() : base() { }
    func name() -> string = "dog"
}

Runs `run` → 42

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

namespace Test

abstract ref data Top {
    init() { }
    abstract func go() -> int
}

abstract ref data Middle : Top {
    init() : base() { }
}

ref data Leaf : Middle {
    init() : base() { }
    : func go() -> int = 42
}

func run() -> int { return Leaf().go() }

Compiles

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

namespace Test

open ref data Animal {
    init() { }
    virtual func speak() -> string = "<silence>"
}

Rejected at compile time: ES2126

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Virtual_Func_On_Sealed_Class_Reports_ES2126   topic: inheritance   status: verified
// verified behavior: reports diagnostic ES2126

namespace Test

ref data Cat {
    init() { }
    virtual func meow() -> string = "meow"
}

Compiles

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

namespace Test

open ref data Computer {
    init() {}
    virtual func step() -> int = 1
    virtual func run() -> int = self.step() + self.step()
}

ref data Faster : Computer {
    init() : base() {}
    : func step() -> int = 10
}

func test() -> int {
    let f = Faster()
    return f.run()
}

Runs `test` → 25

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

namespace Test

abstract ref data Shape {
    init() {}
    abstract func area() -> int
}

ref data Square : Shape {
    pub side: int
    init(s: int) : base() { self.side = s }
    : func area() -> int = self.side * self.side
}

func test() -> int {
    let sq = Square(5)
    return sq.area()
}

Runs `test` → "alice"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Inheritance_Base_Constructor_With_Args   topic: inheritance   status: verified
// verified behavior: Test.test(...) == "alice"

namespace Test

open ref data Named {
    pub name: string
    init(name: string) { self.name = name }
}

ref data Greet : Named {
    init(name: string) : base(name) {}
}

func test() -> string {
    let g = Greet("alice")
    return g.name
}

Runs `test` → "dog"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Inheritance_Three_Level_Chain_Pin   topic: inheritance   status: verified
// verified behavior: Test.test(...) == "dog"

namespace Test

open ref data Animal {
    pub kind: string
    init(k: string) { self.kind = k }
}

open ref data Mammal : Animal {
    init(k: string) : base(k) {}
}

ref data Dog : Mammal {
    init() : base("dog") {}
}

func test() -> string {
    let d = Dog()
    return d.kind
}

Runs `test` → "ABC"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::RefData_Virtual_Method_Overridden_Multiple_Levels_Pin   topic: inheritance   status: verified
// verified behavior: Test.test(...) == "ABC"

namespace Test

open ref data A {
    init() {}
    virtual func name() -> string = "A"
}

open ref data B : A {
    init() : base() {}
    : func name() -> string = "B"
}

ref data C : B {
    init() : base() {}
    : func name() -> string = "C"
}

func test() -> string {
    let a = A()
    let b = B()
    let c = C()
    return a.name() + b.name() + c.name()
}

Runs `test` → "noise"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Virtual_Method_Default_Used_When_Not_Overridden   topic: inheritance   status: verified
// verified behavior: Test.test(...) == "noise"

namespace Test

open ref data Animal {
    init() {}
    virtual func sound() -> string = "noise"
}

func test() -> string {
    let a = Animal()
    return a.sound()
}

ILEmitterTests3__Virtual_Method_Override_Wins

inheritance runnable verified

Runs `test` → "woof"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Virtual_Method_Override_Wins   topic: inheritance   status: verified
// verified behavior: Test.test(...) == "woof"

namespace Test

open ref data Animal {
    init() {}
    virtual func sound() -> string = "noise"
}

ref data Dog : Animal {
    init() : base() {}
    : func sound() -> string = "woof"
}

func test() -> string {
    let d = Dog()
    return d.sound()
}

Rejected at compile time: ES2125

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Abstract_Func_Outside_Abstract_Class_Reports_ES2125   topic: inheritance   status: unverified
// verified behavior: reports diagnostic ES2125

namespace Test

open ref data Animal {
    init() { }
    abstract func voice() -> string
}

Rejected at compile time: ES2124

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Inheritance.cs::Colon_Marker_Without_Inheritance_Header_Is_ES2124   topic: inheritance   status: unverified
// verified behavior: reports diagnostic ES2124

namespace Test

ref data Solo {
    init() { }
    : func go() -> int = 1
}