Add pattern matching on variable bindings based on lazy invariant syntax
bvssvni opened this issue · comments
Got this idea from #640
Look up in arrays:
b := [1, 2, 3]
a : [_, ...] = b // first (1)
c : [.., _, ...] = b // second (2)
d : [..., _] = b // last (3)
e : [..., _, ..] = b // second last (2)
f : [.., .., _] = b // reports runtime error if `b` has not length `3`
b := ok("hi")
a : ok(_) = b // reports runtime error if `b` is `err`.
b := [1, 2]
a : [1, _] = b // gets second item in list if the first is `1`.
c : [a, _] = b // gets second item in list if the first is equal to `a`.
An idea is to bind multiple variables by repeating _
.
c := [1, 2]
a, b : [_, _] = c
Another idea is to add an if
version that lets you bind variables without reporting a runtime error:
c := [1, 2]
if a, b : [_, _] = c { ... }
An alternative syntax is to borrow from path semantics and use (= a)
for equality check:
c := [1, 2]
[a, b] := c // declare `a` and `b`
[(= a), (= b)] := c // check equality with `a` and `b`