0
0
mirror of https://github.com/vim/vim.git synced 2025-10-09 06:14:17 -04:00

patch 8.2.2204: Vim9: using -> both for method and lambda is confusing

Problem:    Vim9: using -> both for method and lambda is confusing.
Solution:   Use => for lambda in :def function.
This commit is contained in:
Bram Moolenaar
2020-12-24 15:14:01 +01:00
parent b34f337472
commit 65c4415276
5 changed files with 246 additions and 44 deletions

View File

@@ -1887,6 +1887,100 @@ def Test_expr7_lambda()
CheckDefFailure(['var Fx = {a -> [0', ' 1]}'], 'E696:', 2)
enddef
def NewLambdaWithComments(): func
return (x) =>
# some comment
x == 1
# some comment
||
x == 2
enddef
def NewLambdaUsingArg(x: number): func
return () =>
# some comment
x == 1
# some comment
||
x == 2
enddef
def Test_expr7_new_lambda()
var lines =<< trim END
var La = () => 'result'
assert_equal('result', La())
assert_equal([1, 3, 5], [1, 2, 3]->map((key, val) => key + val))
# line continuation inside lambda with "cond ? expr : expr" works
var ll = range(3)
map(ll, (k, v) => v % 2 ? {
['111']: 111 } : {}
)
assert_equal([{}, {111: 111}, {}], ll)
ll = range(3)
map(ll, (k, v) => v == 8 || v
== 9
|| v % 2 ? 111 : 222
)
assert_equal([222, 111, 222], ll)
ll = range(3)
map(ll, (k, v) => v != 8 && v
!= 9
&& v % 2 == 0 ? 111 : 222
)
assert_equal([111, 222, 111], ll)
var dl = [{key: 0}, {key: 22}]->filter(( _, v) => v['key'] )
assert_equal([{key: 22}], dl)
dl = [{key: 12}, {['foo']: 34}]
assert_equal([{key: 12}], filter(dl,
(_, v) => has_key(v, 'key') ? v['key'] == 12 : 0))
assert_equal(false, NewLambdaWithComments()(0))
assert_equal(true, NewLambdaWithComments()(1))
assert_equal(true, NewLambdaWithComments()(2))
assert_equal(false, NewLambdaWithComments()(3))
assert_equal(false, NewLambdaUsingArg(0)())
assert_equal(true, NewLambdaUsingArg(1)())
var res = map([1, 2, 3], (i: number, v: number) => i + v)
assert_equal([1, 3, 5], res)
# Lambda returning a dict
var Lmb = () => {key: 42}
assert_equal({key: 42}, Lmb())
END
CheckDefSuccess(lines)
CheckDefFailure(["var Ref = (a)=>a + 1"], 'E1001:')
CheckDefFailure(["var Ref = (a)=> a + 1"], 'E1001:')
CheckDefFailure(["var Ref = (a) =>a + 1"], 'E1001:')
CheckDefFailure(["filter([1, 2], (k,v) => 1)"], 'E1069:', 1)
# error is in first line of the lambda
CheckDefFailure(["var L = (a) -> a + b"], 'E1001:', 1)
# TODO: lambda after -> doesn't work yet
# assert_equal('xxxyyy', 'xxx'->((a, b) => a .. b)('yyy'))
# CheckDefExecFailure(["var s = 'asdf'->{a -> a}('x')"],
# 'E1106: One argument too many')
# CheckDefExecFailure(["var s = 'asdf'->{a -> a}('x', 'y')"],
# 'E1106: 2 arguments too many')
# CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:', 1)
CheckDefSuccess(['var Fx = (a) => {k1: 0,', ' k2: 1}'])
CheckDefFailure(['var Fx = (a) => {k1: 0', ' k2: 1}'], 'E722:', 2)
CheckDefFailure(['var Fx = (a) => {k1: 0,', ' k2 1}'], 'E720:', 2)
CheckDefSuccess(['var Fx = (a) => [0,', ' 1]'])
CheckDefFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2)
enddef
def Test_expr7_lambda_vim9script()
var lines =<< trim END
vim9script