|
1 |
| -*vim9.txt* For Vim version 9.1. Last change: 2023 Dec 09 |
| 1 | +*vim9.txt* For Vim version 9.1. Last change: 2024 Jan 12 |
2 | 2 |
|
3 | 3 |
|
4 | 4 | VIM REFERENCE MANUAL by Bram Moolenaar
|
@@ -1055,8 +1055,11 @@ variable, since they cannot be deleted with `:unlet`. E.g.: >
|
1055 | 1055 |
|
1056 | 1056 | The values can also be useful as the default value for an argument: >
|
1057 | 1057 | def MyFunc(b: blob = null_blob)
|
1058 |
| - if b == null_blob |
1059 |
| - # b argument was not given |
| 1058 | + # Note: compare against null, not null_blob, |
| 1059 | + # to distinguish the default value from an empty blob. |
| 1060 | + if b == null |
| 1061 | + # b argument was not given |
| 1062 | +See |null-compare| for more information about testing against null. |
1060 | 1063 |
|
1061 | 1064 | It is possible to compare `null` with any value, this will not give a type
|
1062 | 1065 | error. However, comparing `null` with a number, float or bool will always
|
@@ -1257,10 +1260,12 @@ Script-local variables in a |Vim9| script must be declared at the script
|
1257 | 1260 | level. They cannot be created in a function, also not in a legacy function.
|
1258 | 1261 |
|
1259 | 1262 | *:defc* *:defcompile*
|
1260 |
| -:defc[ompile] Compile functions defined in the current script that |
1261 |
| - were not compiled yet. |
1262 |
| - This will report any errors found during compilation. |
1263 |
| - This excludes functions defined inside a class. |
| 1263 | +:defc[ompile] Compile functions and classes (|class-compile|) |
| 1264 | + defined in the current script that were not compiled |
| 1265 | + yet. This will report any errors found during |
| 1266 | + compilation. |
| 1267 | + |
| 1268 | +:defc[ompile] MyClass Compile all methods in a class |class-compile|. |
1264 | 1269 |
|
1265 | 1270 | :defc[ompile] {func}
|
1266 | 1271 | :defc[ompile] debug {func}
|
@@ -1698,6 +1703,155 @@ argument type checking: >
|
1698 | 1703 | Types are checked for most builtin functions to make it easier to spot
|
1699 | 1704 | mistakes.
|
1700 | 1705 |
|
| 1706 | +Categories of variables, defaults and null handling ~ |
| 1707 | + *variable-categories* *null-variables* |
| 1708 | +There are categories of variables: |
| 1709 | + primitive number, float, boolean |
| 1710 | + container string, blob, list, dict |
| 1711 | + specialized function, job, channel, user-defined-object |
| 1712 | + |
| 1713 | +When declaring a variable without an initializer, an explicit type must be |
| 1714 | +provided. Each category has different default initialization semantics. Here's |
| 1715 | +an example for each category: > |
| 1716 | + var num: number # primitives default to a 0 equivalent |
| 1717 | + var cont: list<string> # containers default to an empty container |
| 1718 | + var spec: job # specialized variables default to null |
| 1719 | +< |
| 1720 | +Vim does not have a familiar null value; it has various null_<type> predefined |
| 1721 | +values, for example |null_string|, |null_list|, |null_job|. Primitives do not |
| 1722 | +have a null_<type>. The typical use cases for null_<type> are: |
| 1723 | +- to `clear a variable` and release its resources; |
| 1724 | +- as a `default for a parameter` in a function definition, see |null-compare|. |
| 1725 | + |
| 1726 | +For a specialized variable, like `job`, null_<type> is used to clear the |
| 1727 | +resources. For a container variable, resources can also be cleared by |
| 1728 | +assigning an empty container to the variable. For example: > |
| 1729 | + var j: job = job_start(...) |
| 1730 | + # ... job does its work |
| 1731 | + j = null_job # clear the variable and release the job's resources |
| 1732 | +
|
| 1733 | + var l: list<any> |
| 1734 | + # ... add lots of stuff to list |
| 1735 | + l = [] # clear the variable and release container resources |
| 1736 | +Using the empty container, rather than null_<type>, to clear a container |
| 1737 | +variable may avoid null complications as described in |null-anomalies|. |
| 1738 | + |
| 1739 | +The initialization semantics of container variables and specialized variables |
| 1740 | +differ. An uninitialized container defaults to an empty container: > |
| 1741 | + var l1: list<string> # empty container |
| 1742 | + var l2: list<string> = [] # empty container |
| 1743 | + var l3: list<string> = null_list # null container |
| 1744 | +"l1" and "l2" are equivalent and indistinguishable initializations; but "l3" |
| 1745 | +is a null container. A null container is similar to, but different from, an |
| 1746 | +empty container, see |null-anomalies|. |
| 1747 | + |
| 1748 | +Specialized variables default to null. These job initializations are |
| 1749 | +equivalent and indistinguishable: > |
| 1750 | + var j1: job |
| 1751 | + var j2: job = null_job |
| 1752 | + var j3 = null_job |
| 1753 | +
|
| 1754 | +When a list or dict is declared, if the item type is not specified and can not |
| 1755 | +be inferred, then the type is "any": > |
| 1756 | + var d1 = {} # type is "dict<any>" |
| 1757 | + var d2 = null_dict # type is "dict<any>" |
| 1758 | +
|
| 1759 | +Declaring a function, see |vim9-func-declaration|, is particularly unique. |
| 1760 | + |
| 1761 | + *null-compare* |
| 1762 | +For familiar null compare semantics, where a null container is not equal to |
| 1763 | +an empty container, do not use null_<type> in a comparison: > |
| 1764 | + vim9script |
| 1765 | + def F(arg: list<string> = null_list) |
| 1766 | + if arg == null |
| 1767 | + echo "null" |
| 1768 | + else |
| 1769 | + echo printf("not null, %sempty", empty(arg) ? '' : 'not ') |
| 1770 | + endif |
| 1771 | + enddef |
| 1772 | + F() # output: "null" |
| 1773 | + F(null_list) # output: "null" |
| 1774 | + F([]) # output: "not null, empty" |
| 1775 | + F(['']) # output: "not null, not empty" |
| 1776 | +The above function takes a `list of strings` and reports on it. |
| 1777 | +Change the above function signature to accept different types of arguments: > |
| 1778 | + def F(arg: list<any> = null_list) # any type of list |
| 1779 | + def F(arg: any = null) # any type |
| 1780 | +< |
| 1781 | +In the above example, where the goal is to distinguish a null list from an |
| 1782 | +empty list, comparing against `null` instead of `null_list` is the correct |
| 1783 | +choice. The basic reason is because "null_list == null" and "[] != null". |
| 1784 | +Comparing to `null_list` fails since "[] == null_list". In the following section |
| 1785 | +there are details about comparison results. |
| 1786 | + |
| 1787 | + *null-details* *null-anomalies* |
| 1788 | +This section describes issues about using null and null_<type>; included below |
| 1789 | +are the enumerated results of null comparisons. In some cases, if familiar |
| 1790 | +with vim9 null semantics, the programmer may chose to use null_<type> in |
| 1791 | +comparisons and/or other situations. |
| 1792 | + |
| 1793 | +Elsewhere in the documentation it says: |
| 1794 | + Quite often a null value is handled the same as an |
| 1795 | + empty value, but not always |
| 1796 | +Here's an example: > |
| 1797 | + vim9script |
| 1798 | + var s1: list<string> |
| 1799 | + var s2: list<string> = null_list |
| 1800 | + echo s1 # output: "[]" |
| 1801 | + echo s2 # output: "[]" |
| 1802 | + |
| 1803 | + echo s1 + ['a'] # output: "['a']" |
| 1804 | + echo s2 + ['a'] # output: "['a']" |
| 1805 | + |
| 1806 | + echo s1->add('a') # output: "['a']" |
| 1807 | + echo s2->add('a') # E1130: Can not add to null list |
| 1808 | +< |
| 1809 | +Two values equal to a null_<type> are not necessarily equal to each other: > |
| 1810 | + vim9script |
| 1811 | + echo {} == null_dict # true |
| 1812 | + echo null_dict == null # true |
| 1813 | + echo {} == null # false |
| 1814 | +< |
| 1815 | +Unlike the other containers, an uninitialized string is equal to null. The |
| 1816 | +'is' operator can be used to determine if it is a null_string: > |
| 1817 | + vim9script |
| 1818 | + var s1: string |
| 1819 | + var s2 = null_string |
| 1820 | + echo s1 == null # true - this is unexpected |
| 1821 | + echo s2 == null # true |
| 1822 | + echo s2 is null_string # true |
| 1823 | +
|
| 1824 | + var b1: blob |
| 1825 | + var b2 = null_blob |
| 1826 | + echo b1 == null # false |
| 1827 | + echo b2 == null # true |
| 1828 | +< |
| 1829 | +Any variable initialized to the null_<type> is equal to the null_<type> and is |
| 1830 | +also equal to null. For example: > |
| 1831 | + vim9script |
| 1832 | + var x = null_blob |
| 1833 | + echo x == null_blob # true |
| 1834 | + echo x == null # true |
| 1835 | +< |
| 1836 | +An uninitialized variable is usually equal to null; it depends on its type: |
| 1837 | + var s: string s == null |
| 1838 | + var b: blob b != null *** |
| 1839 | + var l: list<any> l != null *** |
| 1840 | + var d: dict<any> d != null *** |
| 1841 | + var f: func f == null |
| 1842 | + var j: job j == null |
| 1843 | + var c: channel c == null |
| 1844 | + var o: Class o == null |
| 1845 | + |
| 1846 | +A variable initialized to empty equals null_<type>; but not null: |
| 1847 | + var s2: string = "" == null_string != null |
| 1848 | + var b2: blob = 0z == null_blob != null |
| 1849 | + var l2: list<any> = [] == null_list != null |
| 1850 | + var d2: dict<any> = {} == null_dict != null |
| 1851 | + |
| 1852 | +NOTE: the specialized variables, like job, default to null value and have no |
| 1853 | +corresponding empty value. |
| 1854 | + |
1701 | 1855 | ==============================================================================
|
1702 | 1856 |
|
1703 | 1857 | 5. Namespace, Import and Export
|
|
0 commit comments