Skip to content

generics — examples

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

Compiles

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

namespace Test
func identity<T>(value: T) -> T {
    return value
}
func go() -> int {
    return identity<int>(99)
}

Runs `go` → "Box { item = 5 }"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::DeriveBoth_EqualityAndDebug   topic: generics   status: verified
// verified behavior: Test.go(...) == "Box { item = 5 }"

derive equality, debug
data Box<T> {
  item: T
}
func go() -> string {
  let a = Box<int> { item: 5 }
  return a.ToString()
}

Runs `go` → "Flag { on = True }"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericDerive.cs::GenericDebug_BoolField   topic: generics   status: verified
// verified behavior: Test.go(...) == "Flag { on = True }"

derive debug
data Flag<T> {
  on: T
}
func go() -> string {
  let f = Flag<bool> { on: true }
  return f.ToString()
}

Runs `go` → true

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

derive equality
data Box<T> {
  item: T
}
func go() -> bool {
  let a = Box<int> { item: 42 }
  let b = Box<int> { item: 42 }
  return a.Equals(b)
}

Runs `test` → 42

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

namespace Test

data Box<T> { value: T }

func test() -> int {
    let b = Box<int> { value: 42 }
    return b.value
}

Runs `test` → 42

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

namespace Test

func id<T>(x: T) -> T = x

func test() -> int = id<int>(42)

Runs `test` → 43

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Generic_Identity_Function_Int_LetBinding   topic: generics   status: verified
// verified behavior: Test.test(...) == 43

namespace Test

func id<T>(x: T) -> T = x

func test() -> int {
    let b = id<int>(42)
    return b + 1
}

Runs `test` → "hello"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Generic_Identity_Function_String   topic: generics   status: verified
// verified behavior: Test.test(...) == "hello"

namespace Test

func id<T>(x: T) -> T = x

func test() -> string = id<string>("hello")

Runs `test` → "hi!"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Generic_Identity_Function_String_LetBinding   topic: generics   status: verified
// verified behavior: Test.test(...) == "hi!"

namespace Test

func id<T>(x: T) -> T = x

func test() -> string {
    let s = id<string>("hi")
    return s + "!"
}

Runs `test` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Generic_Identity_Function_UserData_LetBinding   topic: generics   status: verified
// verified behavior: Test.test(...) == 7

namespace Test

data Box { n: int }
func id<T>(x: T) -> T = x

func test() -> int {
    let b = id<Box>(Box { n: 7 })
    return b.n
}

Compiles

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

namespace Util

func first<T>(items: List<T>) -> T {
    return items[0]
}

func swap<A, B>(a: A, b: B) -> B {
    return b
}