match: fix typo and remove arrows

pull/2606/head
ytakahashi 2019-11-01 00:58:19 +09:00 committed by Alexander Medvednikov
parent 7fa33fc250
commit 114953f28d
2 changed files with 35 additions and 31 deletions

View File

@ -3372,7 +3372,7 @@ fn (p mut Parser) switch_statement() {
'https://vlang.io/docs#match') 'https://vlang.io/docs#match')
} }
// Returns typ if used as expession // Returns typ if used as expression
fn (p mut Parser) match_statement(is_expr bool) string { fn (p mut Parser) match_statement(is_expr bool) string {
p.check(.key_match) p.check(.key_match)
p.cgen.start_tmp() p.cgen.start_tmp()
@ -3566,7 +3566,7 @@ fn (p mut Parser) match_statement(is_expr bool) string {
if is_expr { if is_expr {
// we get here if no else found, ternary requires "else" branch // we get here if no else found, ternary requires "else" branch
p.error('Match expession requires "else"') p.error('Match expression requires "else"')
} }
p.returns = false // only get here when no default, so return is not guaranteed p.returns = false // only get here when no default, so return is not guaranteed

View File

@ -3,38 +3,40 @@ enum Color{
} }
fn test_match_integers() { fn test_match_integers() {
// a := 3 mut a := 3
// mut b := 0 mut b := 0
// match a { match a {
// 2 => println('two') 2 { println('two') }
// 3 => println('three') 3 {
// b = 3 println('three')
// 4 => println('four') b = 3
// else => println('???') }
// } 4 { println('four') }
// assert b == 3 else { println('???') }
}
assert b == 3
assert match 2 { assert match 2 {
1 => {2} 1 { 2 }
2 => {3} 2 { 3 }
else => {5} else { 5 }
} == 3 } == 3
assert match 0 { assert match 0 {
1 => {2} 1 { 2 }
2 => {3} 2 { 3 }
else => 5 else 5
} == 5 } == 5
assert match 1 { assert match 1 {
else => {5} else { 5 }
} == 5 } == 5
mut a := 0 a = 0
match 2 { match 2 {
0 => {a = 1} 0 { a = 1 }
1 => {a = 2} 1 { a = 2 }
else => { else {
a = 3 a = 3
println('a is $a') println('a is $a')
} }
@ -43,8 +45,8 @@ fn test_match_integers() {
a = 0 a = 0
match 1 { match 1 {
0 => {a = 1} 0 { a = 1 }
1 => { 1 {
a = 2 a = 2
a = a + 2 a = a + 2
a = a + 2 a = a + 2
@ -54,7 +56,7 @@ fn test_match_integers() {
a = 0 a = 0
match 1 { match 1 {
else => { else {
a = -2 a = -2
} }
} }
@ -64,11 +66,13 @@ fn test_match_integers() {
fn test_match_enums(){ fn test_match_enums(){
mut b := Color.red mut b := Color.red
match b{ match b{
.red => { .red {
b = .green b = .green
} }
.green => {b = .blue} .green {
else => { b = .blue
}
else {
println('b is ${b.str()}') println('b is ${b.str()}')
b = .red b = .red
} }
@ -76,10 +80,10 @@ fn test_match_enums(){
assert b == .green assert b == .green
match b{ match b{
.red => { .red {
b = .green b = .green
} }
else => { else {
println('b is ${b.str()}') println('b is ${b.str()}')
b = .blue b = .blue
} }