|
1 |
| -*usr_52.txt* For Vim version 8.2. Last change: 2022 May 13 |
| 1 | +*usr_52.txt* For Vim version 8.2. Last change: 2022 May 21 |
2 | 2 |
|
3 | 3 | VIM USER MANUAL - by Bram Moolenaar
|
4 | 4 |
|
@@ -110,35 +110,36 @@ Although it's shorter to do: >
|
110 | 110 | ==============================================================================
|
111 | 111 | *52.3* Functions and types
|
112 | 112 |
|
113 |
| -Legacy Vim script does have type checking, but this happens at runtime, when |
114 |
| -the code is executed. And it's permissive, often a computation gives an |
115 |
| -unexpected value instead of reporting an error. Thus you can define a |
116 |
| -function and think it's fine, but see a problem only later when it is called: > |
117 |
| - let s:collected = '' |
118 |
| - func ExtendAndReturn(add) |
119 |
| - let s:collected += a:add |
120 |
| - return s:collected |
| 113 | +Legacy Vim script only checks types at runtime, when the code is executed. |
| 114 | +And it's permissive, often a computation gives an unexpected value instead of |
| 115 | +reporting an error. Thus you can define a function and think it's fine, but |
| 116 | +notice a problem only later when the function is called: > |
| 117 | + func Concatenate(base, add) |
| 118 | + return a:base + a:add |
121 | 119 | endfunc
|
122 | 120 |
|
123 | 121 | Can you spot the error? Try this: >
|
124 |
| - echo ExtendAndReturn('text') |
125 |
| -And you'll see zero. Why? Because in legacy Vim script "+=" will convert the |
126 |
| -arguments to numbers, and any string without a number results in zero! |
127 |
| - |
128 |
| -With `:def` the type checking happens when compiling the function. For that |
129 |
| -you need to specify the argument types and the return type. Also notice that |
130 |
| -the argument is used without the "a:" prefix: > |
131 |
| - let s:collected = '' |
132 |
| - def ExtendAndReturn(add: string): string |
133 |
| - s:collected += add |
134 |
| - return s:collected |
| 122 | + echo Concatenate('base', 'text') |
| 123 | +And you'll see zero. Why? Because in legacy Vim script "+" will convert the |
| 124 | +arguments to numbers, and any string without a number results in zero! That's |
| 125 | +not what you expected. |
| 126 | + |
| 127 | +With `:def` the type checking happens when compiling the function. You need |
| 128 | +to specify the argument types and the return type to make that possible. Also |
| 129 | +notice that the argument names are used without the "a:" prefix: > |
| 130 | + def Concatenate(base: string, add: string): string |
| 131 | + return base + add |
135 | 132 | enddef
|
136 |
| - defcompile |
| 133 | + defcompile Concatenate |
137 | 134 |
|
138 | 135 | Here we use `:defcompile` to do the compilation right away, without it the
|
139 |
| -compilation would happen when the function is called. Vim will tell you what |
140 |
| -you did wrong: > |
141 |
| - E1013: type mismatch, expected number but got string |
| 136 | +compilation would happen when the function is first called. Vim will tell you |
| 137 | +what you did wrong: > |
| 138 | + E1051: Wrong argument type for + |
| 139 | +
|
| 140 | +Side note: here the context is legacy script. When using Vim9 script you |
| 141 | +would put `:defcompile` at the end of the script to check for errors in all |
| 142 | +the functions defined in it. |
142 | 143 |
|
143 | 144 | Vim9 script is strict, it uses the "+" operator only for numbers and floats.
|
144 | 145 | For string concatenation ".." must be used. This avoids mistakes and avoids
|
|
0 commit comments