4.7 KiB
META
description=Match expression with list rest patterns testing variable scoping
type=expr
SOURCE
match items {
[first, ..rest] => first + 1
[..rest, last] => last + 2
[x, ..rest, y] => x + y
}
EXPECTED
BAD LIST REST PATTERN SYNTAX - list_rest_scoping.md:2:13:2:19 BAD LIST REST PATTERN SYNTAX - list_rest_scoping.md:3:6:3:12 BAD LIST REST PATTERN SYNTAX - list_rest_scoping.md:4:9:4:15 UNDEFINED VARIABLE - list_rest_scoping.md:1:7:1:12 UNUSED VARIABLE - list_rest_scoping.md:2:15:2:15 UNUSED VARIABLE - list_rest_scoping.md:3:8:3:8 UNUSED VARIABLE - list_rest_scoping.md:4:11:4:11
PROBLEMS
BAD LIST REST PATTERN SYNTAX
List rest patterns should use the .. as name syntax, not ..name.
For example, use [first, .. as rest] instead of [first, ..rest].
list_rest_scoping.md:2:13:2:19:
[first, ..rest] => first + 1
^^^^^^
BAD LIST REST PATTERN SYNTAX
List rest patterns should use the .. as name syntax, not ..name.
For example, use [first, .. as rest] instead of [first, ..rest].
list_rest_scoping.md:3:6:3:12:
[..rest, last] => last + 2
^^^^^^
BAD LIST REST PATTERN SYNTAX
List rest patterns should use the .. as name syntax, not ..name.
For example, use [first, .. as rest] instead of [first, ..rest].
list_rest_scoping.md:4:9:4:15:
[x, ..rest, y] => x + y
^^^^^^
UNDEFINED VARIABLE
Nothing is named items in this scope.
Is there an import or exposing missing up-top?
list_rest_scoping.md:1:7:1:12:
match items {
^^^^^
UNUSED VARIABLE
Variable rest is not used anywhere in your code.
If you don't need this variable, prefix it with an underscore like _rest to suppress this warning.
The unused variable is declared here:
list_rest_scoping.md:2:15:2:15:
[first, ..rest] => first + 1
^
UNUSED VARIABLE
Variable rest is not used anywhere in your code.
If you don't need this variable, prefix it with an underscore like _rest to suppress this warning.
The unused variable is declared here:
list_rest_scoping.md:3:8:3:8:
[..rest, last] => last + 2
^
UNUSED VARIABLE
Variable rest is not used anywhere in your code.
If you don't need this variable, prefix it with an underscore like _rest to suppress this warning.
The unused variable is declared here:
list_rest_scoping.md:4:11:4:11:
[x, ..rest, y] => x + y
^
TOKENS
KwMatch,LowerIdent,OpenCurly,
OpenSquare,LowerIdent,Comma,DoubleDot,LowerIdent,CloseSquare,OpFatArrow,LowerIdent,OpPlus,Int,
OpenSquare,DoubleDot,LowerIdent,Comma,LowerIdent,CloseSquare,OpFatArrow,LowerIdent,OpPlus,Int,
OpenSquare,LowerIdent,Comma,DoubleDot,LowerIdent,Comma,LowerIdent,CloseSquare,OpFatArrow,LowerIdent,OpPlus,LowerIdent,
CloseCurly,
EndOfFile,
PARSE
(e-match
(e-ident (raw "items"))
(branches
(branch
(p-list
(p-ident (raw "first"))
(p-list-rest (name "rest")))
(e-binop (op "+")
(e-ident (raw "first"))
(e-int (raw "1"))))
(branch
(p-list
(p-list-rest (name "rest"))
(p-ident (raw "last")))
(e-binop (op "+")
(e-ident (raw "last"))
(e-int (raw "2"))))
(branch
(p-list
(p-ident (raw "x"))
(p-list-rest (name "rest"))
(p-ident (raw "y")))
(e-binop (op "+")
(e-ident (raw "x"))
(e-ident (raw "y"))))))
FORMATTED
match items {
[first, .. as rest] => first + 1
[.. as rest, last] => last + 2
[x, .. as rest, y] => x + y
}
CANONICALIZE
(e-match
(match
(cond
(e-runtime-error (tag "ident_not_in_scope")))
(branches
(branch
(patterns
(pattern (degenerate false)
(p-list
(patterns
(p-assign (ident "first")))
(rest-at (index 1)
(p-assign (ident "rest"))))))
(value
(e-binop (op "add")
(e-lookup-local
(p-assign (ident "first")))
(e-num (value "1")))))
(branch
(patterns
(pattern (degenerate false)
(p-list
(patterns
(p-assign (ident "last")))
(rest-at (index 0)
(p-assign (ident "rest"))))))
(value
(e-binop (op "add")
(e-lookup-local
(p-assign (ident "last")))
(e-num (value "2")))))
(branch
(patterns
(pattern (degenerate false)
(p-list
(patterns
(p-assign (ident "x"))
(p-assign (ident "y")))
(rest-at (index 1)
(p-assign (ident "rest"))))))
(value
(e-binop (op "add")
(e-lookup-local
(p-assign (ident "x")))
(e-lookup-local
(p-assign (ident "y")))))))))
TYPES
(expr (type "a where [a.from_numeral : Numeral -> Try(a, [InvalidNumeral(Str)])]"))