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 6fa6dfbbc0 - Show all commits

View File

@ -490,7 +490,7 @@ statements and functions definitions. If a guard expression evaluates to
will be checked: will be checked:
```sophia ```sophia
function get_left_if_positive(x : one_or_both('a, 'b)) : option('a) = function get_left_if_positive(x : one_or_both(int, 'b)) : option(int) =
switch(x) switch(x)
Left(x) | x > 0 => Some(x) Left(x) | x > 0 => Some(x)
Both(x, _) | x > 0 => Some(x) Both(x, _) | x > 0 => Some(x)
@ -499,10 +499,10 @@ function get_left_if_positive(x : one_or_both('a, 'b)) : option('a) =
```sophia ```sophia
function function
get_left_if_positive : one_or_both('a, 'b) => option('a) get_left_if_positive : one_or_both(int, 'b) => option(int)
get_left(Left(x)) | x > 0 = Some(x) get_left_if_positive(Left(x)) | x > 0 = Some(x)
get_left(Both(x, _)) | x > 0 = Some(x) get_left_if_positive(Both(x, _)) | x > 0 = Some(x)
get_left(_) = None get_left_if_positive(_) = None
``` ```
Guards cannot be stateful even when used inside a stateful function. Guards cannot be stateful even when used inside a stateful function.