Allow assigning patterns to variables (#336)

* Change syntax tree and parser

* Add assign pattern to type inference

* Use check_expr instead of hard-coded type

* Add fcode generation for assign pattern

* Implement rename_spat for assign pattern

* Add tests

* Update CHANGELOG.md

* Update docs and changelog

* Add letpat to aeso_syntax_utils:fold

* Use Plus instead of Scoped
This commit is contained in:
Gaith Hallak
2021-09-11 17:18:30 +03:00
committed by GitHub
parent a7b7aafced
commit e8a171dc45
11 changed files with 68 additions and 4 deletions
+11
View File
@@ -471,6 +471,17 @@ function
get_left(Both(x, _)) = Some(x)
```
Sophia also supports the assignment of patterns to variables:
```sophia
function f(x) = switch(x)
h1::(t = h2::_) => (h1 + h2)::t // same as `h1::h2::k => (h1 + h2)::h2::k`
_ => x
function g(p : int * option(int)) : int =
let (a, (o = Some(b))) = p // o is equal to Pair.snd(p)
b
```
*NOTE: Data types cannot currently be recursive.*
## Lists