aa3433b5a9
- Add ekGenericCall to AST for generic function calls (Max<int>)
- Parse generic type arguments in parsePostfix
- Support generic calls in sema with type parameter substitution
- Implement monomorphization in hir_lower:
- Collect generic function declarations
- Find all generic call sites
- Generate specialized versions with mangled names (Max_int)
- Substitute type parameters with concrete types
- Add generics.bux example
Example:
func Max<T>(a: T, b: T) -> T {
if a > b { return a; }
else { return b; }
}
let m: int = Max<int>(10, 20); // Generates Max_int
27 lines
529 B
Plaintext
27 lines
529 B
Plaintext
// Generics - Generic functions and structs
|
|
extern func Std_Io_PrintLine(s: String);
|
|
extern func Std_Io_PrintInt(n: int);
|
|
|
|
func Max<T>(a: T, b: T) -> T {
|
|
if a > b {
|
|
return a;
|
|
} else {
|
|
return b;
|
|
}
|
|
}
|
|
|
|
func Main() -> int {
|
|
let m1: int = Max<int>(10, 20);
|
|
let m2: int = Max<int>(5, 3);
|
|
|
|
Std_Io_PrintLine("Max(10, 20) = ");
|
|
Std_Io_PrintInt(m1);
|
|
Std_Io_PrintLine("");
|
|
|
|
Std_Io_PrintLine("Max(5, 3) = ");
|
|
Std_Io_PrintInt(m2);
|
|
Std_Io_PrintLine("");
|
|
|
|
return 0;
|
|
}
|