1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix declare assign literal with closure (#14544)

This commit is contained in:
yuyi 2022-05-28 16:50:37 +08:00 committed by GitHub
parent 4894f61998
commit a46cf10e92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -305,7 +305,7 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
}
}
}
left_type_unwrapped := c.unwrap_generic(left_type)
left_type_unwrapped := c.unwrap_generic(ast.mktyp(left_type))
right_type_unwrapped := c.unwrap_generic(right_type)
if right_type_unwrapped == 0 {
// right type was a generic `T`

View File

@ -0,0 +1,26 @@
fn sma(period int) fn (f64) f64 {
mut i := 0
mut sum := 0.0
mut storage := []f64{len: 0, cap: period}
return fn [mut storage, mut sum, mut i, period] (input f64) f64 {
if storage.len < period {
sum += input
storage << input
}
sum += input - storage[i]
storage[i], i = input, (i + 1) % period
return sum / f64(storage.len)
}
}
fn test_assign_literal_with_closure() {
sma3 := sma(3)
sma5 := sma(5)
println('x sma3 sma5')
for x in [f64(1), 2, 3, 4, 5, 5, 4, 3, 2, 1] {
println('${x:5.3f} ${sma3(x):5.3f} ${sma5(x):5.3f}')
}
assert true
}