runtime mutable global state
This commit is contained in:
@@ -44,11 +44,11 @@ main func() i32 {
|
||||
total = total + 5 # 35
|
||||
}
|
||||
|
||||
# block scoping: inner x shadows outer x, outer is unchanged after the block
|
||||
# block scoping: inner bindings do not escape the block
|
||||
x i32 = 1
|
||||
if x == 1 {
|
||||
x i32 = 100
|
||||
if x == 100 {
|
||||
inner_x i32 = 100
|
||||
if inner_x == 100 {
|
||||
total = total + 5 # 40
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
bad int = 1
|
||||
bad i32 :: undefined
|
||||
|
||||
read_bad func() int {
|
||||
return bad
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
bad int = 1
|
||||
bad i32 :: undefined
|
||||
|
||||
read_bad func() int {
|
||||
return bad
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
Point :: struct {
|
||||
x i32
|
||||
}
|
||||
|
||||
counter int = 0
|
||||
ratio float = 1
|
||||
span range = 0..2
|
||||
point Point = Point { x = 1 }
|
||||
values [_]mut i32 = [10, 20]
|
||||
|
||||
bump func(value @mut i32) void {
|
||||
value^ += 1
|
||||
}
|
||||
|
||||
main func() i32 {
|
||||
counter = 10
|
||||
counter += 5
|
||||
bump(&counter)
|
||||
point.x += counter
|
||||
values[1] = point.x
|
||||
|
||||
total i32 = counter + point.x + values[1]
|
||||
for span |i| {
|
||||
total += i
|
||||
}
|
||||
if ratio == 1.0 {
|
||||
total += 1
|
||||
}
|
||||
return total
|
||||
}
|
||||
@@ -31,12 +31,12 @@ main func() i32 {
|
||||
}
|
||||
}
|
||||
|
||||
# The body-local k shadows only inside the body. The update still targets
|
||||
# Body-local storage stays scoped to the body. The update still targets
|
||||
# the mutable k declared before the loop.
|
||||
k u32 = 0
|
||||
while k < 4 : k = k + 1 {
|
||||
k u32 = 100
|
||||
if k == 100 {
|
||||
body_k u32 = 100
|
||||
if body_k == 100 {
|
||||
total = total + 2
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user