Allow assigning patterns to variables #827

Merged
ghallak merged 10 commits from ghallak/231 into master 2021-09-11 23:18:30 +09:00
2 changed files with 12 additions and 1 deletions
Showing only changes of commit 27e2170545 - Show all commits

View File

@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Set` stdlib - `Set` stdlib
- `Option.force_msg` - `Option.force_msg`
- Loading namespaces into the current scope (e.g. `using Pair`) - Loading namespaces into the current scope (e.g. `using Pair`)
- Assign patterns to variables - Assign patterns to variables (e.g. `let x::(t = y::_) = [1, 2, 3, 4]` where `t == [2, 3, 4]`)
### Changed ### Changed
### Removed ### Removed

View File

@ -471,6 +471,17 @@ function
get_left(Both(x, _)) = Some(x) 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.* *NOTE: Data types cannot currently be recursive.*
## Lists ## Lists