-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathswitch.jl
49 lines (45 loc) · 1.1 KB
/
switch.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@testcase "@switch" begin
flag = Ref(0)
function try_setflag(x)
@switch x begin
@case (::Integer, 2)
flag[] = 1
@case ::Bool
nothing
flag[] = 2
@case ::String
flag[] = 0
end
end
try_setflag((1, 2))
@test flag[] == 1
try_setflag(false)
@test flag[] == 2
try_setflag("")
@test flag[] == 0
# Non-exhaustive matches throw an error.
@test_throws ErrorException try_setflag(:a)
flag2 = Ref(0)
function try_setflag_2(x)
@tryswitch x begin
@case (::Integer, 2)
flag[] = 1
@case ::Bool
flag[] = 2
@tryswitch x begin
@case true
flag2[] = 2
end
end
end
try_setflag_2((1, 2))
@test flag[] == 1
try_setflag_2(false)
@test flag[] == 2
@test flag2[] == 0
try_setflag_2(true)
@test flag[] == 2
@test flag2[] == 2
# Non-exhaustive matches fail silently.
try_setflag_2("")
end