-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from serenity4/tryswitch
Implement @tryswitch
- Loading branch information
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |