struct Texture { name: string uses: int func touch() { uses += 1 } } struct Sprite { tex: shared Texture x: int } struct Window { title: string buttons: []shared Button func add(b: shared Button) { buttons.push(b) } } struct Button { label: string window: weak Window? clicks: int func click() { clicks += 1 if window != nil { print(label, "clicked in", window.title) } else { print(label, "clicked, window is gone") } } } func use_texture(t: shared Texture) { t.touch() } func main() { // several owners of one value: a change through one is seen by all var tex = shared Texture{"grass", 0} var a = Sprite{tex, 1} var b = Sprite{tex, 2} a.tex.touch() b.tex.touch() use_texture(tex) print(tex.name, tex.uses, a.tex.uses, b.tex.uses) print(a) // a window and its buttons point at each other: the button's side is weak var win = shared Window{"Main", []} var ok = shared Button{"OK", weak win, 0} win.add(ok) ok.click() win.buttons[0].click() print(ok.clicks) var cancel = shared Button{label: "Cancel"} print(cancel.window == nil) cancel.window = weak win print(cancel.window == nil) cancel.click() // once the window is gone, the weak pointers know var saved = ok win = shared Window{"Other", []} saved.click() cancel.click() }