Last active
May 22, 2024 15:44
-
-
Save sa-/6be55a8c90934a01cd443503650b5e0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @value | |
| struct State[T: AnyRegType]: | |
| var _value: T | |
| fn get(self) -> T: | |
| return self._value | |
| fn set(inout self, value: T): | |
| self._value = value | |
| @value | |
| struct Computed[inferred T: AnyRegType, func: fn () capturing -> T]: | |
| fn get(self) -> T: | |
| return func() | |
| fn main() raises: | |
| var s = State(0) | |
| # Ideally we would write | |
| # var c = Computed[lambda: s.get() * 2]() | |
| # but we're taking the scenic route for now | |
| @parameter | |
| fn double() capturing -> Int: | |
| return s.get() * 2 | |
| var c = Computed[double]() | |
| print(s.get(), c.get()) | |
| s.set(1) | |
| print(s.get(), c.get()) | |
| s.set(2) | |
| print(s.get(), c.get()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment