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

@ -121,7 +121,7 @@ contract IntHolder =
type state = int type state = int
entrypoint init(x) = x entrypoint init(x) = x
entrypoint get() = state entrypoint get() = state
main contract IntHolderFactory = main contract IntHolderFactory =
stateful entrypoint new(x : int) : IntHolder = stateful entrypoint new(x : int) : IntHolder =
let ih = Chain.create(x) : IntHolder let ih = Chain.create(x) : IntHolder
@ -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
@ -851,4 +870,4 @@ Some chain operations (`Oracle.<operation>` and `AENS.<operation>`) have an
optional delegation signature. This is typically used when a user/accounts optional delegation signature. This is typically used when a user/accounts
would like to allow a contract to act on it's behalf. The exact data to be would like to allow a contract to act on it's behalf. The exact data to be
signed varies for the different operations, but in all cases you should prepend signed varies for the different operations, but in all cases you should prepend
the signature data with the `network_id` (`ae_mainnet` for the æternity mainnet, etc.). the signature data with the `network_id` (`ae_mainnet` for the æternity mainnet, etc.).