Skip to content

Instantly share code, notes, and snippets.

@sa-
Last active May 22, 2024 15:44
Show Gist options
  • Save sa-/6be55a8c90934a01cd443503650b5e0b to your computer and use it in GitHub Desktop.
Save sa-/6be55a8c90934a01cd443503650b5e0b to your computer and use it in GitHub Desktop.
@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