umka-lang
umka-lang copied to clipboard
Allow weak pointers pointing to stack or global storage
Weak pointers are now only allowed for heap-allocated data:
fn f() {
a := 42
wp := weak ^int(&a)
if p := ^int(wp); p != null {
printf(repr(p^) + '\n')
} else {
printf("NULL\n")
}
}
fn g() {
q := new(int, 42)
wp := weak ^int(q)
if p := ^int(wp); p != null {
printf(repr(p^) + '\n')
} else {
printf("NULL\n")
}
}
fn main() {
f() // NULL
g() // 42
}
From the language specification:
If the weak pointer does not point to a valid dynamically allocated memory block, the result of this conversion is
null.
Related issue: #274