mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix fixed array of chan (#18438)
This commit is contained in:
parent
75f325b950
commit
06583be9ec
@ -201,6 +201,21 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st
|
||||
elem_type: arr_info.elem_type
|
||||
})
|
||||
}
|
||||
} else if elem_sym.kind == .chan {
|
||||
// fixed array for chan -- [N]chan
|
||||
info := array_type.unaliased_sym.info as ast.ArrayFixed
|
||||
chan_info := elem_sym.chan_info()
|
||||
g.expr(ast.ChanInit{
|
||||
typ: node.elem_type
|
||||
elem_type: chan_info.elem_type
|
||||
})
|
||||
for _ in 1 .. info.size {
|
||||
g.write(', ')
|
||||
g.expr(ast.ChanInit{
|
||||
typ: node.elem_type
|
||||
elem_type: chan_info.elem_type
|
||||
})
|
||||
}
|
||||
} else {
|
||||
g.write('0')
|
||||
}
|
||||
|
31
vlib/v/tests/fixed_array_chan_test.v
Normal file
31
vlib/v/tests/fixed_array_chan_test.v
Normal file
@ -0,0 +1,31 @@
|
||||
import time
|
||||
|
||||
struct Struct {
|
||||
ch chan int
|
||||
}
|
||||
|
||||
fn listen(ch chan int) {
|
||||
for {
|
||||
t := <-ch or { break }
|
||||
println(t)
|
||||
}
|
||||
}
|
||||
|
||||
fn sp_array() {
|
||||
mut array := [3]chan int{}
|
||||
for a in 0 .. 3 {
|
||||
spawn listen(array[a])
|
||||
}
|
||||
for i, a in array {
|
||||
a <- i
|
||||
}
|
||||
time.sleep(1000 * time.millisecond)
|
||||
for c in array {
|
||||
c.close()
|
||||
}
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
sp_array()
|
||||
assert true
|
||||
}
|
Loading…
Reference in New Issue
Block a user