From 9c9f62238345d67e79016d39d161de7ff62c6b58 Mon Sep 17 00:00:00 2001 From: S42yt Date: Fri, 13 Nov 2026 14:38:28 +0000 Subject: [PATCH] Document generic methods and generic interfaces --- content/changelog.mdx | 1 + content/language/generics.mdx | 73 +++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/content/changelog.mdx b/content/changelog.mdx index a62ef73..cccb5ee 100644 --- a/content/changelog.mdx +++ b/content/changelog.mdx @@ -11,6 +11,7 @@ releases can be downloaded from [GitHub](https://github.com/burnlang/burn/releas ## Unreleased +- Generic methods and generic interfaces: `fun map(f: fun(T): U): Box` in a struct, static or not, and `def interface Shows` implemented with `:: Shows` or, in a generic struct, `:: Shows` ([generics](./language/generics.mdx#generic-methods)). - Windows: `burn build` makes native Windows x86-64 executables, and the whole toolchain runs on Windows: `burn.exe`, `burni.exe`, `burnc.exe`, `burn-lsp.exe`, `bvm.exe` and `burnfmt.cmd`, with the JIT, async tasks, processes (`cmd /c` for `Process.shell`), HTTP and stack overflow reports. Releases include `burn-windows-x86_64.zip`, built from Linux with mingw-w64 and tested on Windows in CI ([installation](./tooling/installation.mdx#windows), [cross-compiling](./tooling/cross-compiling.mdx)). - `@Extern` takes a Windows name next to the macOS one (`windows: "_errno"`), and C functions can take more than 6 integer parameters ([calling C](./language/calling-c.mdx)). - Native code is much faster ([native executables](./internals/native.mdx#a-function)): the hottest locals in loops live in registers, parameters are no longer copied on every call, `push` stores in place, division by a constant uses a multiplication instead of the divide instruction, small constants are immediates, and small functions are inlined. A prime sieve runs 8× faster and pushing to an array 20× faster, on x86-64 and arm64. diff --git a/content/language/generics.mdx b/content/language/generics.mdx index 9034e25..50100a2 100644 --- a/content/language/generics.mdx +++ b/content/language/generics.mdx @@ -83,6 +83,72 @@ print(s.pop(), s.pop(), s.pop()) - Values print with their type arguments: `Pair { first: "one", second: 1 }`. - A generic struct can implement interfaces, and then works wherever the interface is expected. +## Generic methods + +A method can have type parameters of its own, in a generic struct or an ordinary one, static or not. Like a generic +function, it gets its types from the arguments or from the type the result is assigned to: + +```burn +def struct Box(value: T) { + fun map(f: fun(T): U): Box { + return new Box(f(value)) + } +} + +def struct Tools { + static fun firstOf(xs: [T]): T { + return xs[0] + } +} + +var b = new Box(2) +print(b.map(fun(x: int): string { + return "v" + str(x) +}).value) // v2 +print(Tools.firstOf(["a", "b"])) // a +``` + +Generic methods are inherited by structs that extend the struct, and inside the struct they can be called without +`self`. `init` cannot have type parameters. + +## Generic interfaces + +An interface can have type parameters. Each instance, such as `Shows`, is an interface type of its own; a struct +implements an instance with `::`, and a generic struct can implement one with its own parameter: + +```burn +def interface Shows { + fun show(x: T): string +} + +def struct IntShow :: Shows { + fun show(x: int): string { + return "#" + str(x) + } +} + +def struct Echo(tag: string) :: Shows { + fun show(x: T): string { + return "${tag}:${x}" + } +} + +fun showAll(s: Shows, xs: [T]): [string] { + [string] out = [] + for x in xs { + out.push(s.show(x)) + } + return out +} + +Shows s = new IntShow() +print(s.show(3)) // #3 +print(showAll(new Echo("f"), [0.5])) // ["f:0.5"] +``` + +`showAll` gets `T` from the interface instance it is given. Writing `:: Shows` without type arguments is an error +that suggests one. + ## What a type parameter can do There are no constraints to declare. A type parameter can be used in any way that every type it is actually used @@ -130,8 +196,7 @@ types. This is what C++ and Rust do, and it means: ## Limits -- Methods cannot have their own type parameters; only the struct's parameters can be used in them. Write a generic - function outside the struct instead and call it with [method syntax](./functions.mdx#method-syntax). - Generic structs cannot extend another struct, and cannot have static members. -- Enums, interfaces and type aliases (`def type Name = ...`) cannot take type parameters. -- Type arguments cannot be written on a function call (`first(xs)`); give the argument a type instead. +- Enums and type aliases (`def type Name = ...`) cannot take type parameters. +- Type arguments cannot be written on a function or method call (`first(xs)`); give the argument or the result a + type instead.