generics — examples
← all topics · 50 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` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericConformance.cs::PlainGeneric_RefData_Construct topic: generics status: verified
// verified behavior: Test.go(...) == 42
namespace Test
class Box2<T> {
v: T
init(x: T) { self.v = x }
func get() -> T = self.v
}
func go() -> int { let b = Box2<int>(42) return b.get() }ILEmitterTests_GenericConformance__RefData_Generic_EsharpGenericInterface
generics runnable verifiedRuns `go` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericConformance.cs::RefData_Generic_EsharpGenericInterface topic: generics status: verified
// verified behavior: Test.go(...) == 42
namespace Test
interface IBox<T> { func get() -> T }
class Box<T> : IBox<T> {
v: T
init(x: T) { self.v = x }
func get() -> T = self.v
}
func read(b: IBox<int>) -> int = b.get()
func go() -> int { let b = Box<int>(42) return read(b) }ILEmitterTests_GenericConformance__ValueData_Generic_EsharpGenericInterface
generics runnable verifiedRuns `go` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_GenericConformance.cs::ValueData_Generic_EsharpGenericInterface topic: generics status: verified
// verified behavior: Test.go(...) == 7
namespace Test
interface IHolder<T> { func value() -> T }
struct Holder<T> : IHolder<T> { item: T }
func (h: Holder<T>) value<T>() -> T = h.item
func use(h: IHolder<int>) -> int = h.value()
func go() -> int { let h = Holder<int> { item: 7 } return use(h) }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
struct 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
struct 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
struct Box<T> {
item: T
}
func go() -> bool {
let a = Box<int> { item: 42 }
let b = Box<int> { item: 42 }
return a.Equals(b)
}Runs `go` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Integration.cs::CrossNamespace_QualifiedGenericFreeFunction topic: generics status: verified
// verified behavior: Test.go(...) == 5
namespace Lib
func identity<T>(x: T) -> T {
return x
}Runs `go` → 42
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Nominal.cs::Added_GenericConformance_GenericInterface topic: generics status: verified
// verified behavior: Test.go(...) == 42
namespace Test
interface IBoxOf<T> { func get() -> T }
class Box<T> : IBoxOf<T> {
value: T
init(v: T) { self.value = v }
func get() -> T = self.value
}
func read(b: IBoxOf<int>) -> int = b.get()
func go() -> int { let b = Box<int>(42) return read(b) }Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Nominal.cs::Added_GenericConformance_IEnumerable topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
using "System.Collections"
using "System.Collections.Generic"
class Seq<T> : IEnumerable<T>, IEnumerable {
items: List<T>
init(xs: List<T>) { self.items = xs }
func GetEnumerator() -> IEnumerator<T> = self.items.GetEnumerator()
}
func make() -> Seq<int> {
var xs = List<int>()
xs.Add(3)
xs.Add(4)
return Seq<int>(xs)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_Receivers.cs::GenericValueReceiver_MapsAcrossTypeArgs topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
struct Wrap<T> { v: T }
func (w: Wrap<T>) mapped<T, U>(f: Func<T, U>) -> Wrap<U> = Wrap<U> { v: f(w.v) }
func run() -> int {
let w = Wrap<int> { v: 3 }
return w.mapped((x) => x + 5).v
}Runs `go` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::A01_GenericValueData_TwoParams topic: generics status: verified
// verified behavior: Test.go(...) == 7
namespace Test
struct Res<T, E> { ok: bool, v: T, e: E }
func go() -> int {
let r = Res<int, string> { ok: true, v: 7, e: "" }
return r.v
}Runs `go` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::A02_GenericValueData_MethodReturnsT topic: generics status: verified
// verified behavior: Test.go(...) == 5
namespace Test
struct Box<T> { item: T }
func (b: Box<T>) unwrap<T>() -> T = b.item
func go() -> int {
let b = Box<int> { item: 5 }
return b.unwrap()
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::A03_DeriveEquality_TwoParam_Equal topic: generics status: verified
// verified behavior: Test.go(...) == true
namespace Test
derive equality
struct Res<T, E> { ok: bool, v: T, e: E }
func go() -> bool {
let a = Res<int, string> { ok: true, v: 7, e: "" }
let b = Res<int, string> { ok: true, v: 7, e: "" }
return a.Equals(b)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::A04_DeriveEquality_EmitsIEquatable topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
derive equality
struct Res<T, E> { ok: bool, v: T, e: E }
func make() -> Res<int, string> = Res<int, string> { ok: true, v: 1, e: "" }Runs `go` → "Box { item = 5 }"
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::A06_DeriveDebug_Generic topic: generics status: verified
// verified behavior: Test.go(...) == "Box { item = 5 }"
namespace Test
derive debug
struct Box<T> { item: T }
func go() -> string {
let b = Box<int> { item: 5 }
return b.ToString()
}Runs `go` → true
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::A07_ValueData_GenericInterface_ParamT topic: generics status: verified
// verified behavior: Test.go(...) == true
namespace Test
interface IEq<T> { func eq(other: T) -> bool }
struct Num<T> : IEq<T> { v: int }
func (n: Num<T>) eq<T>(other: T) -> bool = true
func use(x: IEq<int>) -> bool = x.eq(0)
func go() -> bool {
let n = Num<int> { v: 3 }
return use(n)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::B01_GenericRefData_IDisposable topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
using "System"
class Res<T> : IDisposable {
v: T
init(x: T) { self.v = x }
func Dispose() { }
}
func make() -> Res<int> = Res<int>(1)Runs `go` → 5
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::B02_GenericRefData_WrapsListT topic: generics status: verified
// verified behavior: Test.go(...) == 5
namespace Test
using "System.Collections.Generic"
class Wrap<T> {
items: List<T>
init() { self.items = List<T>() }
func add(x: T) { self.items.Add(x) }
func first() -> T = self.items[0]
}
func go() -> int {
let w = Wrap<int>()
w.add(5)
return w.first()
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::B03_GenericRefData_WrapsChannelT topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
using "System.Threading.Channels"
class Ch<T> {
inner: Channel<T>
init() { self.inner = Channel.CreateUnbounded<T>() }
func writer() -> ChannelWriter<T> = self.inner.Writer
func reader() -> ChannelReader<T> = self.inner.Reader
}
func make() -> Ch<int> = Ch<int>()ILEmitterTests_StdlibReadiness__B04_GenericRefData_IEnumerable_InterfaceEnumeratorReturn
generics unknown verifiedCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::B04_GenericRefData_IEnumerable_InterfaceEnumeratorReturn topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
using "System.Collections"
using "System.Collections.Generic"
class Seq<T> : IEnumerable<T>, IEnumerable {
src: IEnumerable<T>
init(s: IEnumerable<T>) { self.src = s }
func GetEnumerator() -> IEnumerator<T> = self.src.GetEnumerator()
func IEnumerable.GetEnumerator() -> IEnumerator = self.src.GetEnumerator()
}
func make() -> Seq<int> {
var xs = List<int>()
xs.Add(3)
xs.Add(4)
return Seq<int>(xs)
}ILEmitterTests_StdlibReadiness__B05_GenericRefData_IEnumerable_ValueEnumeratorReturn
generics unknown verifiedCompiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::B05_GenericRefData_IEnumerable_ValueEnumeratorReturn topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
using "System.Collections"
using "System.Collections.Generic"
class Seq<T> : IEnumerable<T>, IEnumerable {
items: List<T>
init(xs: List<T>) { self.items = xs }
func GetEnumerator() -> IEnumerator<T> = self.items.GetEnumerator()
func IEnumerable.GetEnumerator() -> IEnumerator = self.items.GetEnumerator()
}
func make() -> Seq<int> {
var xs = List<int>()
xs.Add(3)
xs.Add(4)
return Seq<int>(xs)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::B06_GenericRefData_IAsyncEnumerable topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
using "System.Collections.Generic"
using "System.Threading"
class AStream<T> : IAsyncEnumerable<T> {
src: IAsyncEnumerable<T>
init(s: IAsyncEnumerable<T>) { self.src = s }
func GetAsyncEnumerator(ct: CancellationToken) -> IAsyncEnumerator<T> = self.src.GetAsyncEnumerator(ct)
}ILEmitterTests_StdlibReadiness__B08_GenericRefData_TwoEsharpGenericInterfaces
generics runnable verifiedRuns `go` → 11
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::B08_GenericRefData_TwoEsharpGenericInterfaces topic: generics status: verified
// verified behavior: Test.go(...) == 11
namespace Test
interface ISender<T> { func send(x: T) }
interface IReceiver<T> { func recv() -> T }
class Pipe<T> : ISender<T>, IReceiver<T> {
var slot: T
init(seed: T) { self.slot = seed }
func send(x: T) { self.slot = x }
func recv() -> T = self.slot
}
func go() -> int {
let p = Pipe<int>(0)
let s: ISender<int> = p
s.send(11)
let r: IReceiver<int> = p
return r.recv()
}Runs `go` → 3
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::C01_GenericMethod_OnGenericType topic: generics status: verified
// verified behavior: Test.go(...) == 3
namespace Test
class Box<T> {
v: T
init(x: T) { self.v = x }
func count<U>(other: U) -> int = 3
}
func go() -> int {
let b = Box<int>(1)
return b.count<string>("x")
}Runs `go` → 2
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::C03_NestedGenericField topic: generics status: verified
// verified behavior: Test.go(...) == 2
namespace Test
using "System.Collections.Generic"
class Index<T> {
map: Dictionary<string, List<T>>
init() { self.map = Dictionary<string, List<T>>() }
func put(k: string, v: T) {
let lst = List<T>()
lst.Add(v)
self.map[k] = lst
}
func size(k: string) -> int = self.map[k].Count
}
func go() -> int {
let ix = Index<int>()
ix.put("a", 10)
ix.put("a", 20)
return ix.size("a") + 1
}Runs `go` → 4
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::C05_ValueData_GenericInterface_Boxes topic: generics status: verified
// verified behavior: Test.go(...) == 4
namespace Test
interface ICount<T> { func count() -> int }
struct Bag<T> : ICount<T> { n: int }
func (b: Bag<T>) count<T>() -> int = b.n
func use(c: ICount<int>) -> int = c.count()
func go() -> int {
let b = Bag<int> { n: 4 }
return use(b)
}Runs `go` → 1
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::C06_GenericRefData_NonGenericInterface topic: generics status: verified
// verified behavior: Test.go(...) == 1
namespace Test
interface ITagged { func tag() -> int }
class Box<T> : ITagged {
v: T
init(x: T) { self.v = x }
func tag() -> int = 1
}
func use(t: ITagged) -> int = t.tag()
func go() -> int {
let b = Box<int>(5)
return use(b)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::C07_GenericRefData_IComparableOfT topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
using "System"
class Ver<T> : IComparable<Ver<T>> {
n: int
init(x: int) { self.n = x }
func CompareTo(other: Ver<T>) -> int = self.n - other.n
}
func make() -> Ver<int> = Ver<int>(3)Runs `go` → 6
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::C08_ClosedGenericInstance_InCollection topic: generics status: verified
// verified behavior: Test.go(...) == 6
namespace Test
using "System.Collections.Generic"
class Box<T> {
v: T
init(x: T) { self.v = x }
func get() -> T = self.v
}
func go() -> int {
var xs = List<Box<int>>()
xs.Add(Box<int>(6))
return xs[0].get()
}Runs `go` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::D03_GenericStaticMethod topic: generics status: verified
// verified behavior: Test.go(...) == 7
namespace Test
static Ops {
func identity<T>(x: T) -> T = x
}
func go() -> int = Ops.identity<int>(7)Runs `go` → 13
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::D05_ExplicitGenericInterfaceMember topic: generics status: verified
// verified behavior: Test.go(...) == 13
namespace Test
interface IBox<T> { func get() -> T }
class Box<T> : IBox<T> {
v: T
init(x: T) { self.v = x }
func IBox<T>.get() -> T = self.v
}
func read(b: IBox<int>) -> int = b.get()
func go() -> int {
let b = Box<int>(13)
return read(b)
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::D06_GenericRefData_IEnumerableOfT_Only topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
using "System.Collections.Generic"
class Seq<T> : IEnumerable<T> {
src: IEnumerable<T>
init(s: IEnumerable<T>) { self.src = s }
func GetEnumerator() -> IEnumerator<T> = self.src.GetEnumerator()
}
func make() -> Seq<int> {
var xs = List<int>()
xs.Add(5)
return Seq<int>(xs)
}Runs `go` → 47
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::G21_DataAndStaticFunc_SameName_Coexist topic: generics status: verified
// verified behavior: Test.go(...) == 47
namespace Test
struct Box<T, E> { v: T, e: E }
static Box { func tag() -> int = 42 }
func go() -> int {
let b = Box<int, string> { v: 5, e: "" }
return b.v + Box.tag()
}Runs `go` → 0
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::H01_FreeFunc_ReturnOnly_ExplicitTypeArg topic: generics status: verified
// verified behavior: Test.go(...) == 0
namespace Test
func zero<T>() -> T = default(T)
func go() -> int = zero<int>()Runs `go` → 7
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::H03_GenericCall_ThenMemberAccess topic: generics status: verified
// verified behavior: Test.go(...) == 7
namespace Test
struct Box<T> { v: T }
func boxed<T>(x: T) -> Box<T> = Box<T> { v: x }
func go() -> int = boxed<int>(7).vRuns `go` → 8
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::H07_NonResult_PromotedGenericMap topic: generics status: verified
// verified behavior: Test.go(...) == 8
namespace Test
struct Wrap<T> { v: T }
func (w: Wrap<T>) mapped<T, U>(f: Func<T, U>) -> Wrap<U> = Wrap<U> { v: f(w.v) }
func go() -> int {
let w = Wrap<int> { v: 3 }
let r = w.mapped((x) => x + 5)
return r.v
}Runs `go` → 47
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::J11_Arity_DataAndStaticFunc topic: generics status: verified
// verified behavior: Test.go(...) == 47
namespace Test
struct Tag<T> { v: T }
static Tag { func id() -> int = 42 }
func go() -> int {
let t = Tag<int> { v: 5 }
return t.v + Tag.id()
}Runs `go` → 9
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests_StdlibReadiness.cs::J13_NestedGenericData topic: generics status: verified
// verified behavior: Test.go(...) == 9
namespace Test
struct Box<T> { v: T }
func go() -> int {
let inner = Box<int> { v: 9 }
let outer = Box<Box<int>> { v: inner }
return outer.v.v
}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
struct 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
struct 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: PrimaryCtorCaptureTests.cs::Header_GenericClass_CapturesGenericParam topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
class Holder<T>(value: T) {
func get() -> T { return value }
}
func run() -> int {
let h = Holder<int>(42)
return h.get()
}Rejected at compile time: ES2148
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TotalBinderTests.cs::UnknownCompositeLiteralType_IsLocatedBinderError topic: generics status: verified
// verified behavior: reports diagnostic ES2148
func id<T>(x: T) -> T {
return x
}
func main() -> 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: verified
// 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
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TypeGrammarTests.cs::ExplicitTypeArgs_CloseDeclaredReturn topic: generics status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func id<T>(x: T) -> T { return x }
func go() -> int {
let n = id<int>(41)
return n + 1
}Runs `go` → 21
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TypeGrammarTests.cs::QualifiedGeneric_CrossNamespaceAnnotation topic: generics status: verified
// verified behavior: Test.go(...) == 21
namespace A
struct Holder<T> { item: T }