Pattern guards for functions and switch statements #830

Merged
ghallak merged 23 commits from ghallak/232 into master 2021-10-20 17:04:01 +09:00
Showing only changes of commit 059059ac87 - Show all commits

View File

@ -482,6 +482,25 @@ function g(p : int * option(int)) : int =
b b
``` ```
Guards are boolean functions that can be used on patterns in both switch
statements and functions definitions:
```sophia
function get_left_if_positive(x : one_or_both('a, 'b)) : option('a) =
switch(x)
Left(x) | x > 0 => Some(x)
Both(x, _) | x > 0 => Some(x)
_ => None
```
```sophia
function
get_left_if_positive : one_or_both('a, 'b) => option('a)
get_left(Left(x)) | x > 0 = Some(x)
get_left(Both(x, _)) | x > 0 = Some(x)
get_left(_) = None
```
*NOTE: Data types cannot currently be recursive.* *NOTE: Data types cannot currently be recursive.*
## Lists ## Lists