af14f392f6
- Auto-register func Type_Method<T>(self: *Type<T>) as methods in sema - Add type param awareness to sema resolveType for generic function bodies - Add lazy monomorphization for generic struct method calls in HIR lowering - Track varTypeExprs in LowerCtx for local variable type inference - Fix ekSelf in both sema and hir_lower to resolve actual parameter type - Fix lowerFunc to use substituteType for param/return types (handles pointers to generic structs) - Auto-address value receivers when method expects pointer (e.g., b.Get() where self: *Box<T>) - Add C forward declarations for all functions to fix ordering issues - Relax type checks for tkTypeParam in assignments and arguments - Update generics_struct example with Box_Get, Box_Set, Pair_GetFirst/Second
52 lines
992 B
Plaintext
52 lines
992 B
Plaintext
// Generic structs - Box<T> and Pair<T, U>
|
|
import Std::Io::{PrintLine, PrintInt};
|
|
|
|
struct Box<T> {
|
|
value: T,
|
|
}
|
|
|
|
struct Pair<T, U> {
|
|
first: T,
|
|
second: U,
|
|
}
|
|
|
|
func Box_Get<T>(self: *Box<T>) -> T {
|
|
return self.value;
|
|
}
|
|
|
|
func Box_Set<T>(self: *Box<T>, value: T) {
|
|
self.value = value;
|
|
}
|
|
|
|
func Pair_GetFirst<T, U>(self: *Pair<T, U>) -> T {
|
|
return self.first;
|
|
}
|
|
|
|
func Pair_GetSecond<T, U>(self: *Pair<T, U>) -> U {
|
|
return self.second;
|
|
}
|
|
|
|
func Main() -> int {
|
|
let b: Box<int> = Box<int> { value: 42 };
|
|
PrintLine("Box value:");
|
|
PrintInt(b.Get());
|
|
PrintLine("");
|
|
|
|
b.Set(100);
|
|
PrintLine("Box after Set(100):");
|
|
PrintInt(b.Get());
|
|
PrintLine("");
|
|
|
|
let p: Pair<int, String> = Pair<int, String> { first: 10, second: "hello" };
|
|
PrintLine("Pair first:");
|
|
PrintInt(p.GetFirst());
|
|
PrintLine("");
|
|
|
|
let bp: *Box<int> = &b;
|
|
PrintLine("Box via pointer:");
|
|
PrintInt(bp.Get());
|
|
PrintLine("");
|
|
|
|
return 0;
|
|
}
|