|
| 1 | +title: Generic Functions and Types |
| 2 | +--- |
| 3 | +<!-- See https://github.com/spylang/spy/pull/519 and https://github.com/spylang/spy/pull/448--> |
| 4 | + |
| 5 | +### Generic Functions |
| 6 | + |
| 7 | +Functions decorated with [@blue.generic](../api_reference/spy_builtins.md#bluegeneric) are called with `[]` brackets instead of parentheses. These *may* be used anywhere, but they are intended to help create functions that look like [PEP 695](https://peps.python.org/pep-0695/) functions with type parameters: |
| 8 | + |
| 9 | +```python |
| 10 | +@blue.generic |
| 11 | +def add(T): |
| 12 | + def impl(x:T, y: T) -> T: |
| 13 | + return x + y |
| 14 | + return impl |
| 15 | + |
| 16 | +def main() -> None: |
| 17 | + print(add[i32](1, 2)) |
| 18 | + print(add[str]('hello ', 'world')) |
| 19 | +``` |
| 20 | + |
| 21 | +Like all functions marked `@blue`, the generic function is guaranteed to be executed at compile-time. We can see in the redshifted version of the above code that `add()` no longer appears, but the two specialized versions of it remain: |
| 22 | + |
| 23 | +<!-- Colorful code formatted by ansi2html --> |
| 24 | +<style type="text/css"> |
| 25 | +.ansi2html-content { display: block; white-space: pre-wrap; word-wrap: break-word; font-size: .85em; padding:1.1em; corner-radius: 0.1em} |
| 26 | +.ansi32 { color: #00aa00; } |
| 27 | +.ansi33 { color: #aa5500; } |
| 28 | +.ansi34 { color: #0000aa; } |
| 29 | +.ansi35 { color: #E850A8; } |
| 30 | +</style> |
| 31 | +<div class="body_background" style="background-color:rgb(245, 245, 245);"> |
| 32 | +<pre class="ansi2html-content"> |
| 33 | +<span class="ansi34">def</span> main() -> <span class="ansi34">None</span>: |
| 34 | + <span class="ansi35">`_print::println[i32]::p`</span>(<span class="ansi35">`t::add[i32]::impl`</span>(<span class="ansi33">1</span>, <span class="ansi33">2</span>)) |
| 35 | + <span class="ansi35">`_print::println[str]::p`</span>(<span class="ansi35">`t::add[str]::impl`</span>(<span class="ansi32">'hello '</span>, <span class="ansi32">'world'</span>)) |
| 36 | + |
| 37 | +<span class="ansi34">def</span> <span class="ansi35">`t::add[i32]::impl`</span>(x: <span class="ansi35">i32</span>, y: <span class="ansi35">i32</span>) -> <span class="ansi35">i32</span>: |
| 38 | + <span class="ansi34">return</span> x + y |
| 39 | + |
| 40 | +<span class="ansi34">def</span> <span class="ansi35">`t::add[str]::impl`</span>(x: <span class="ansi35">str</span>, y: <span class="ansi35">str</span>) -> <span class="ansi35">str</span>: |
| 41 | + <span class="ansi34">return</span> <span class="ansi35">`operator::str_add`</span>(x, y) |
| 42 | +</pre> |
| 43 | +</div> |
| 44 | + |
| 45 | +### Generic Class Syntax |
| 46 | + |
| 47 | +`@struct` classes may also be created with one or more parameters in `[]` brackets. This is different from passing superclasses inside of `()` parentheses; rather, this is syntactic sugar for a generic function with an inner `@struct` class than can make use of those parameters: |
| 48 | + |
| 49 | +```py |
| 50 | +@struct |
| 51 | +class MyList[T]: |
| 52 | + inner: list[T] |
| 53 | + other_param_1: ... |
| 54 | + other_param_2: ... |
| 55 | + |
| 56 | +# ↑ is syntactic sugar for ↓ |
| 57 | + |
| 58 | +@blue.generic |
| 59 | +def MyList(T): |
| 60 | + @struct |
| 61 | + class Self: |
| 62 | + inner: list[T] |
| 63 | + other_param_1: ... |
| 64 | + other_param_2: ... |
| 65 | + |
| 66 | + return Self |
| 67 | +``` |
| 68 | + |
| 69 | +In use, this looks like: |
| 70 | + |
| 71 | +```py |
| 72 | +@struct |
| 73 | +class MyNamedList[T]: |
| 74 | + name: str |
| 75 | + data: list[T] |
| 76 | + |
| 77 | +def main() -> None: |
| 78 | + my_int_list = MyNamedList[i32]("profits", []) |
| 79 | + my_int_list.data.append(1_000_000) |
| 80 | + |
| 81 | + my_str_list = MyNamedList[str]("words", ["hello", "world"]) |
| 82 | + my_str_list.data.extend(["and", "goodbye"]) |
| 83 | +``` |
| 84 | + |
| 85 | +For a larger example, see the `myarray` example [on GitHub](https://github.com/spylang/spy/blob/main/examples/myarray.spy) or [run it in the SPy Playground](https://spylang.github.io/spy/#code=eJydVcFO40AMvecrrHBpd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xrlgti6I6TWfH7SgYt5Ev0DKx6k_A==) |
| 86 | + |
| 87 | +### `__origin__` |
| 88 | + |
| 89 | +The `__origin__` attribute of SPy objects carries information about the generic function which created them, if any. When `blue.generic` defines a `type` or `function` and returns it, the returned object has it's `__origin__` is set to the generic function: |
| 90 | + |
| 91 | +```py |
| 92 | +@blue.generic |
| 93 | +def adder(T): |
| 94 | + @struct |
| 95 | + class impl: |
| 96 | + ... |
| 97 | + |
| 98 | + return impl |
| 99 | + |
| 100 | + |
| 101 | +def main() -> None: |
| 102 | + assert adder[T].__origin__ is adder |
| 103 | +``` |
| 104 | + |
| 105 | +This is a straightforward way to identify that, for example, `MyList[T]` is a 'specialised' version of `MyList` on the type `T`. |
| 106 | + |
| 107 | +(The default value for `__origin__` is `None`. If the object returned by a generic function already has a non-`None` origin, that origin will *not* be overwritten.) |
| 108 | + |
| 109 | +The `__origin__` functions identically with the Generic Class syntax described above: |
| 110 | + |
| 111 | +```py |
| 112 | +@struct |
| 113 | +class MyList[T]: |
| 114 | + inner: list[T] |
| 115 | + |
| 116 | +def main() -> None: |
| 117 | + assert MyList[i32].__origin__ is MyList |
| 118 | +``` |
0 commit comments