diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-08-09 19:02:50 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-08-09 19:02:50 +0200 |
commit | 64d662d5fc2ff8af4dbf399ff02aa9d711cc9312 (patch) | |
tree | e10acf8f148ec9bc01c5077bd3573ec4ac10eceb /runtime | |
parent | 127542bcebeb6480493b09d75a3be1d98a5f7797 (diff) | |
download | vim-git-64d662d5fc2ff8af4dbf399ff02aa9d711cc9312.tar.gz |
patch 8.2.1408: Vim9: type casting not supportedv8.2.1408
Problem: Vim9: type casting not supported.
Solution: Introduce type casting.
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/doc/vim9.txt | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/runtime/doc/vim9.txt b/runtime/doc/vim9.txt index 8ff70595f..7c4a64f97 100644 --- a/runtime/doc/vim9.txt +++ b/runtime/doc/vim9.txt @@ -640,6 +640,35 @@ And classes and interfaces can be used as types: > {not implemented yet} +Variable types and type casting *variable-types* + +Variables declared in Vim9 script or in a `:def` function have a type, either +specified explicitly or inferred from the initialization. + +Global, buffer, window and tab page variables do not have a specific type, the +value can be changed at any time, possibly changing the type. Therefore, in +compiled code the "any" type is assumed. + +This can be a problem when the "any" type is undesired and the actual type is +expected to always be the same. For example, when declaring a list: > + let l: list<number> = [1, g:two] +This will give an error, because "g:two" has type "any". To avoid this, use a +type cast: > + let l: list<number> = [1, <number>g:two] +< *type-casting* +The compiled code will then check that "g:two" is a number at runtime and give +an error if it isn't. This is called type casting. + +The syntax of a type cast is: "<" {type} ">". There cannot be white space +after the "<" or before the ">" (to avoid them being confused with +smaller-than and bigger-than operators). + +The semantics is that, if needed, a runtime type check is performed. The +value is not actually changed. If you need to change the type, e.g. to change +it to a string, use the |string()| function. Or use |str2nr()| to convert a +string to a number. + + Type inference *type-inference* In general: Whenever the type is clear it can be omitted. For example, when |