Files
rexlang/examples/basics/shared.dino
T
Your NameandClaude Sonnet 5.5 eaf9567a36 Dino compiler: language, formatter, language server, docs, editor support
Compiler in Rust (lexer, parser, checker, C codegen), maps, chars,
dino fmt, dino lsp (diagnostics, formatting, outline, definition,
hover, completion, signature help), docs, examples, VS Code/Vim files.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 23:46:12 +01:00

70 lines
1.4 KiB
Plaintext

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()
}