Description
Describe the bug
When pushing to a channel the return of a method call and using or { }
for channel closed detection, compiler thinks or { }
is to detect method option/result which is not the case.
Reproduction Steps
fn writer(c chan i64, delay i64) {
for {
time.sleep(delay)
c <- time.now().unix_milli() or { // thinks `or` is for inexistent call's result/option
break // chan closed
}
}
}
Expected Behavior
Detect the or { ... }
was intended for channel close before push and compile code above.
Current Behavior
code.v:7:32: error: unexpected `or` block, the function `unix_milli` does not return an Option or a Result
Possible Solution
A workaround is adding extra parentheses around function called:
fn writer(c chan i64, delay i64) {
for {
time.sleep(delay)
c <- (time.now().unix_milli()) or { // works adding extra parenthesis
break // chan closed
}
}
}
Fortunately v fmt
didn't remove those extra parentheses now at playground.
A more robust workaround is to define an extra variable milli := ...
to read the time, then push it as c <- milli or { ... }
.
Additional Information/Context
import time
fn writer(c chan i64, delay i64) {
for {
time.sleep(delay)
c <- time.now().unix_milli() or { // thinks `or` is for inexistent call's result/option
// c <- (time.now().unix_milli()) or { // works adding extra parenthesis
break // chan closed
}
}
}
fn reader(c chan i64, max int) {
mut a := []i64{}
for {
a << <-c
if a.len > max {
c.close()
break
}
}
println('${a}')
}
fn main() {
c := chan i64{}
mut t := []thread{}
t << spawn reader(c, 5)
t << spawn writer(c, 10 * time.millisecond)
t.wait()
}
V version
V 0.4.10 299ebdf
Environment details (OS name and version, etc.)
https://play.vlang.io/p/458165e96c
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.