comparison runtime/doc/eval.txt @ 16615:1a911bd57f11 v8.1.1310

patch 8.1.1310: named function arguments are never optional commit https://github.com/vim/vim/commit/42ae78cfff171fbd7412306083fe200245d7a7a6 Author: Bram Moolenaar <Bram@vim.org> Date: Thu May 9 21:08:58 2019 +0200 patch 8.1.1310: named function arguments are never optional Problem: Named function arguments are never optional. Solution: Support optional function arguments with a default value. (Andy Massimino, closes #3952)
author Bram Moolenaar <Bram@vim.org>
date Thu, 09 May 2019 21:15:05 +0200
parents 1eaf34420bb3
children 4790302965fc
comparison
equal deleted inserted replaced
16614:306acde1a598 16615:1a911bd57f11
10918 However, if a composite type is used, such as |List| or |Dictionary| , you can 10918 However, if a composite type is used, such as |List| or |Dictionary| , you can
10919 change their contents. Thus you can pass a |List| to a function and have the 10919 change their contents. Thus you can pass a |List| to a function and have the
10920 function add an item to it. If you want to make sure the function cannot 10920 function add an item to it. If you want to make sure the function cannot
10921 change a |List| or |Dictionary| use |:lockvar|. 10921 change a |List| or |Dictionary| use |:lockvar|.
10922 10922
10923 When not using "...", the number of arguments in a function call must be equal
10924 to the number of named arguments. When using "...", the number of arguments
10925 may be larger.
10926
10927 It is also possible to define a function without any arguments. You must 10923 It is also possible to define a function without any arguments. You must
10928 still supply the () then. 10924 still supply the () then.
10929 10925
10930 It is allowed to define another function inside a function body. 10926 It is allowed to define another function inside a function body.
10927
10928 *optional-function-argument*
10929 You can provide default values for positional named arguments. This makes
10930 them optional for function calls. When a positional argument is not
10931 specified at a call, the default expression is used to initialize it.
10932 This only works for functions declared with |function|, not for lambda
10933 expressions |expr-lambda|.
10934
10935 Example: >
10936 function Something(key, value = 10)
10937 echo a:key .. ": " .. value
10938 endfunction
10939 call Something('empty') "empty: 10"
10940 call Something('key, 20) "key: 20"
10941
10942 The argument default expressions are evaluated at the time of the function
10943 call, not definition. Thus it is possible to use an expression which is
10944 invalid the moment the function is defined. The expressions are are also only
10945 evaluated when arguments are not specified during a call.
10946
10947 You can pass |v:none| to use the default expression. Note that this means you
10948 cannot pass v:none as an ordinary value when an argument has a default
10949 expression.
10950
10951 Example: >
10952 function Something(a = 10, b = 20, c = 30)
10953 endfunction
10954 call Something(1, v:none, 3) " b = 20
10955 <
10956 *E989*
10957 Optional arguments with default expressions must occur after any mandatory
10958 arguments. You can use "..." after all optional named arguments.
10959
10960 It is possible for later argument defaults to refer to prior arguments,
10961 but not the other way around. They must be prefixed with "a:", as with all
10962 arguments.
10963
10964 Example that works: >
10965 :function Okay(mandatory, optional = a:mandatory)
10966 :endfunction
10967 Example that does NOT work: >
10968 :function NoGood(first = a:second, second = 10)
10969 :endfunction
10970 <
10971 When not using "...", the number of arguments in a function call must be equal
10972 to the number of mandatory named arguments. When using "...", the number of
10973 arguments may be larger.
10931 10974
10932 *local-variables* 10975 *local-variables*
10933 Inside a function local variables can be used. These will disappear when the 10976 Inside a function local variables can be used. These will disappear when the
10934 function returns. Global variables need to be accessed with "g:". 10977 function returns. Global variables need to be accessed with "g:".
10935 10978