comparison runtime/doc/eval.txt @ 9686:8c2553beff0f v7.4.2119

commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f Author: Bram Moolenaar <Bram@vim.org> Date: Fri Jul 29 22:15:09 2016 +0200 patch 7.4.2119 Problem: Closures are not supported. Solution: Capture variables in lambdas from the outer scope. (Yasuhiro Matsumoto, Ken Takata)
author Christian Brabandt <cb@256bit.org>
date Fri, 29 Jul 2016 22:30:08 +0200
parents 9f7bcc2c3b97
children 2ea935bdd1a1
comparison
equal deleted inserted replaced
9685:fee40b9f4989 9686:8c2553beff0f
1 *eval.txt* For Vim version 7.4. Last change: 2016 Jul 24 1 *eval.txt* For Vim version 7.4. Last change: 2016 Jul 29
2 2
3 3
4 VIM REFERENCE MANUAL by Bram Moolenaar 4 VIM REFERENCE MANUAL by Bram Moolenaar
5 5
6 6
38 1.1 Variable types ~ 38 1.1 Variable types ~
39 *E712* 39 *E712*
40 There are nine types of variables: 40 There are nine types of variables:
41 41
42 Number A 32 or 64 bit signed number. |expr-number| *Number* 42 Number A 32 or 64 bit signed number. |expr-number| *Number*
43 64-bit Number is available only when compiled with the 43 64-bit Numbers are available only when compiled with the
44 |+num64| feature. 44 |+num64| feature.
45 Examples: -123 0x10 0177 45 Examples: -123 0x10 0177
46 46
47 Float A floating point number. |floating-point-format| *Float* 47 Float A floating point number. |floating-point-format| *Float*
48 {only when compiled with the |+float| feature} 48 {only when compiled with the |+float| feature}
1217 evaluating |expr1|. Lambda expressions differ from |user-functions| in 1217 evaluating |expr1|. Lambda expressions differ from |user-functions| in
1218 the following ways: 1218 the following ways:
1219 1219
1220 1. The body of the lambda expression is an |expr1| and not a sequence of |Ex| 1220 1. The body of the lambda expression is an |expr1| and not a sequence of |Ex|
1221 commands. 1221 commands.
1222 2. The prefix "a:" is optional for arguments. E.g.: > 1222 2. The prefix "a:" should not be used for arguments. E.g.: >
1223 :let F = {arg1, arg2 -> arg1 - arg2} 1223 :let F = {arg1, arg2 -> arg1 - arg2}
1224 :echo F(5, 2) 1224 :echo F(5, 2)
1225 < 3 1225 < 3
1226 1226
1227 The arguments are optional. Example: > 1227 The arguments are optional. Example: >
1228 :let F = {-> 'error function'} 1228 :let F = {-> 'error function'}
1229 :echo F() 1229 :echo F()
1230 < error function 1230 < error function
1231 *closure*
1232 Lambda expressions can access outer scope variables and arguments. This is
1233 often called a closure. Example where "i" a and "a:arg" are used in a lambda
1234 while they exists in the function scope. They remain valid even after the
1235 function returns: >
1236 :function Foo(arg)
1237 : let i = 3
1238 : return {x -> x + i - a:arg}
1239 :endfunction
1240 :let Bar = Foo(4)
1241 :echo Bar(6)
1242 < 5
1231 1243
1232 Examples for using a lambda expression with |sort()|, |map()| and |filter()|: > 1244 Examples for using a lambda expression with |sort()|, |map()| and |filter()|: >
1233 :echo map([1, 2, 3], {idx, val -> val + 1}) 1245 :echo map([1, 2, 3], {idx, val -> val + 1})
1234 < [2, 3, 4] > 1246 < [2, 3, 4] >
1235 :echo sort([3,7,2,1,4], {a, b -> a - b}) 1247 :echo sort([3,7,2,1,4], {a, b -> a - b})
1242 < Handler called 1254 < Handler called
1243 Handler called 1255 Handler called
1244 Handler called 1256 Handler called
1245 1257
1246 Note how execute() is used to execute an Ex command. That's ugly though. 1258 Note how execute() is used to execute an Ex command. That's ugly though.
1259
1260
1261 Lambda expressions have internal names like '<lambda>42'. If you get an error
1262 for a lambda expression, you can find what it is with the following command: >
1263 :function {'<lambda>42'}
1264 See also: |numbered-function|
1247 1265
1248 ============================================================================== 1266 ==============================================================================
1249 3. Internal variable *internal-variables* *E461* 1267 3. Internal variable *internal-variables* *E461*
1250 1268
1251 An internal variable name can be made up of letters, digits and '_'. But it 1269 An internal variable name can be made up of letters, digits and '_'. But it
7492 test_null_string() *test_null_string()* 7510 test_null_string() *test_null_string()*
7493 Return a String that is null. Only useful for testing. 7511 Return a String that is null. Only useful for testing.
7494 7512
7495 test_settime({expr}) *test_settime()* 7513 test_settime({expr}) *test_settime()*
7496 Set the time Vim uses internally. Currently only used for 7514 Set the time Vim uses internally. Currently only used for
7497 timestamps in the history, as they are used in viminfo. 7515 timestamps in the history, as they are used in viminfo, and
7516 for undo.
7498 {expr} must evaluate to a number. When the value is zero the 7517 {expr} must evaluate to a number. When the value is zero the
7499 normal behavior is restored. 7518 normal behavior is restored.
7500 7519
7501 *timer_start()* 7520 *timer_start()*
7502 timer_start({time}, {callback} [, {options}]) 7521 timer_start({time}, {callback} [, {options}])