mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
parent
f4b51d064e
commit
2641a55a63
2 changed files with 32 additions and 0 deletions
|
@ -8,6 +8,9 @@ import v.util
|
||||||
|
|
||||||
fn (mut g Gen) need_tmp_var_in_match(node ast.MatchExpr) bool {
|
fn (mut g Gen) need_tmp_var_in_match(node ast.MatchExpr) bool {
|
||||||
if node.is_expr && node.return_type != ast.void_type && node.return_type != 0 {
|
if node.is_expr && node.return_type != ast.void_type && node.return_type != 0 {
|
||||||
|
if g.inside_struct_init {
|
||||||
|
return true
|
||||||
|
}
|
||||||
if g.table.sym(node.return_type).kind in [.sum_type, .multi_return]
|
if g.table.sym(node.return_type).kind in [.sum_type, .multi_return]
|
||||||
|| node.return_type.has_option_or_result() {
|
|| node.return_type.has_option_or_result() {
|
||||||
return true
|
return true
|
||||||
|
|
29
vlib/v/tests/match_on_return_test.v
Normal file
29
vlib/v/tests/match_on_return_test.v
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
struct Bauds {
|
||||||
|
standard bool
|
||||||
|
bauds u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn max(v u32) !u32 {
|
||||||
|
if v > 38400 {
|
||||||
|
return error('too fast')
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_bauds(bauds u32) !&Bauds {
|
||||||
|
return &Bauds{
|
||||||
|
standard: match bauds {
|
||||||
|
300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400 { true }
|
||||||
|
else { false }
|
||||||
|
}
|
||||||
|
bauds: max(bauds)!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
t := new_bauds(310)!
|
||||||
|
assert '${t}' == '&Bauds{
|
||||||
|
standard: false
|
||||||
|
bauds: 310
|
||||||
|
}'
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue