runtime(doc): Update Section 4 of vim9.txt
Commit:
https://github.com/vim/vim/commit/54cc8201291e6f8860084ce7e7cd5b8069344612
Author: Peter Kenny <[email protected]>
Date: Mon Nov 10 20:29:08 2025 +0000
runtime(doc): Update Section 4 of vim9.txt
closes: https://github.com/vim/vim/issues/18610
Signed-off-by: Peter Kenny <[email protected]>
Signed-off-by: Christian Brabandt <[email protected]>
diff --git a/runtime/doc/tags b/runtime/doc/tags
index 8fd8cbae1..6feb92b2a 100644
--- a/runtime/doc/tags
+++ b/runtime/doc/tags
@@ -8696,6 +8696,7 @@ instanceof() builtin.txt /*instanceof()*
intel-itanium syntax.txt /*intel-itanium*
intellimouse-wheel-problems gui_w32.txt /*intellimouse-wheel-problems*
interactive-functions usr_41.txt /*interactive-functions*
+interface vim9class.txt /*interface*
interfaces-5.2 version5.txt /*interfaces-5.2*
internal-error message.txt /*internal-error*
internal-variables eval.txt /*internal-variables*
@@ -11595,6 +11596,7 @@ vim9-access-modes vim9class.txt
/*vim9-access-modes*
vim9-autoload vim9.txt /*vim9-autoload*
vim9-boolean vim9.txt /*vim9-boolean*
vim9-class vim9class.txt /*vim9-class*
+vim9-class-type vim9.txt /*vim9-class-type*
vim9-classes vim9.txt /*vim9-classes*
vim9-const vim9.txt /*vim9-const*
vim9-curly vim9.txt /*vim9-curly*
@@ -11602,14 +11604,18 @@ vim9-debug repeat.txt /*vim9-debug*
vim9-declaration vim9.txt /*vim9-declaration*
vim9-declarations usr_41.txt /*vim9-declarations*
vim9-differences vim9.txt /*vim9-differences*
+vim9-enum-type vim9.txt /*vim9-enum-type*
+vim9-enumvalue-type vim9.txt /*vim9-enumvalue-type*
vim9-export vim9.txt /*vim9-export*
vim9-false-true vim9.txt /*vim9-false-true*
vim9-final vim9.txt /*vim9-final*
vim9-func-declaration vim9.txt /*vim9-func-declaration*
+vim9-func-type vim9.txt /*vim9-func-type*
vim9-function-defined-later vim9.txt /*vim9-function-defined-later*
vim9-gotchas vim9.txt /*vim9-gotchas*
vim9-ignored-argument vim9.txt /*vim9-ignored-argument*
vim9-import vim9.txt /*vim9-import*
+vim9-interface-type vim9.txt /*vim9-interface-type*
vim9-lambda vim9.txt /*vim9-lambda*
vim9-lambda-arguments vim9.txt /*vim9-lambda-arguments*
vim9-line-continuation vim9.txt /*vim9-line-continuation*
@@ -11618,11 +11624,14 @@ vim9-mix vim9.txt /*vim9-mix*
vim9-namespace vim9.txt /*vim9-namespace*
vim9-no-dict-function vim9.txt /*vim9-no-dict-function*
vim9-no-shorten vim9.txt /*vim9-no-shorten*
+vim9-object-type vim9.txt /*vim9-object-type*
+vim9-partial-declaration vim9.txt /*vim9-partial-declaration*
vim9-rationale vim9.txt /*vim9-rationale*
vim9-reload vim9.txt /*vim9-reload*
vim9-s-namespace vim9.txt /*vim9-s-namespace*
vim9-scopes vim9.txt /*vim9-scopes*
vim9-string-index vim9.txt /*vim9-string-index*
+vim9-typealias-type vim9.txt /*vim9-typealias-type*
vim9-types vim9.txt /*vim9-types*
vim9-unpack-ignore vim9.txt /*vim9-unpack-ignore*
vim9-user-command vim9.txt /*vim9-user-command*
diff --git a/runtime/doc/vim9.txt b/runtime/doc/vim9.txt
index 7941b83ab..6e65ca064 100644
--- a/runtime/doc/vim9.txt
+++ b/runtime/doc/vim9.txt
@@ -1,4 +1,4 @@
-*vim9.txt* For Vim version 9.1. Last change: 2025 Oct 06
+*vim9.txt* For Vim version 9.1. Last change: 2025 Nov 10
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -24,11 +24,12 @@ features in Vim9 script.
------------------------------------------------------------------------------
NOTE: In this vim9.txt help file, the Vim9 script code blocks beginning
- with `vim9script` are Vim9 script syntax highlighted. Also, they are
- sourceable, meaning you can run them to see what they output. To
- source them, use `:'<,'>source` (see |:source-range|), which is done
- by visually selecting the line(s) with |V| and typing `:so`.
- For example, try it on the following Vim9 script: >vim9
+ with `vim9script` (and individual lines starting with `vim9cmd`) are
+ Vim9 script syntax highlighted. Also, they are sourceable, meaning
+ you can run them to see what they output. To source them, use
+ `:'<,'>source` (see |:source-range|), which is done by visually
+ selecting the line(s) with |V| and typing `:so`. For example, try it
+ on the following Vim9 script: >vim9
vim9script
echowindow "Welcome to Vim9 script!"
@@ -1478,38 +1479,72 @@ without arguments. Example: >
==============================================================================
4. Types *vim9-types*
- *E1008* *E1009* *E1010* *E1012*
- *E1013* *E1029* *E1030*
-The following builtin types are supported:
- bool
- number
- float
- string
- blob
- list<{type}>
- dict<{type}>
- object<{type}>
- job
- channel
- tuple<{type}>
- tuple<{type}, {type}, ...>
- tuple<...list<{type}>>
- tuple<{type}, ...list<{type}>>
- func
- func: {type}
- func({type}, ...)
- func({type}, ...): {type}
+
+The following types, each shown with its corresponding internal |v:t_TYPE|
+variable, are supported:
+
+ number |v:t_number|
+ string |v:t_string|
+ func |v:t_func|
+ func: {type} |v:t_func|
+ func({type}, ...) |v:t_func|
+ func({type}, ...): {type} |v:t_func|
+ list<{type}> |v:t_list|
+ dict<{type}> |v:t_dict|
+ float |v:t_float|
+ bool |v:t_bool|
+ none |v:t_none|
+ job |v:t_job|
+ channel |v:t_channel|
+ blob |v:t_blob|
+ class |v:t_class|
+ object |v:t_object|
+ typealias |v:t_typealias|
+ enum |v:t_enum|
+ enumvalue |v:t_enumvalue|
+ tuple<{type}> |v:t_tuple|
+ tuple<{type}, {type}, ...> |v:t_tuple|
+ tuple<...list<{type}>> |v:t_tuple|
+ tuple<{type}, ...list<{type}>> |v:t_tuple|
void
-These types can be used in declarations, but no simple value will actually
-have the "void" type. Trying to use a void (e.g. a function without a
-return value) results in error *E1031* *E1186* .
+ *E1031* *E1186*
+These types can be used in declarations, though no simple value can have the
+"void" type. Trying to use a void as a value results in an error. Examples:
>vim9
-There is no array type, use list<{type}> instead. For a list constant an
-efficient implementation is used that avoids allocating a lot of small pieces
-of memory.
+ vim9script
+ def NoReturnValue(): void
+ enddef
+ try
+ const X: any = NoReturnValue()
+ catch
+ echo v:exception # E1031: Cannot use void value
+ try
+ echo NoReturnValue()
+ catch
+ echo v:exception # E1186: Expression does not result in a ...
+ endtry
+ endtry
+< *E1008* *E1009* *E1010* *E1012*
+Ill-formed declarations and mismatching types result in errors. The following
+are examples of errors E1008, E1009, E1010, and E1012: >vim9
+
+ vim9cmd var l: list
+ vim9cmd var l: list<number
+ vim9cmd var l: list<invalidtype>
+ vim9cmd var l: list<number> = ['42']
+<
+There is no array type. Instead, use either a list or a tuple. Those types
+may also be literals (constants). In the following example, [5, 6] is a list
+literal and (7, ) a tuple literal. The echoed list is a list literal too:
>vim9
+
+ vim9script
+ var l: list<number> = [1, 2]
+ var t: tuple<...list<number>> = (3, 4)
+ echo [l, t, [5, 6], (7, )]
+<
*tuple-type*
-A tuple type can be declared in more or less specific ways:
+A tuple type may be declared in the following ways:
tuple<number> a tuple with a single item of type |Number|
tuple<number, string> a tuple with two items of type |Number| and
|String|
@@ -1535,7 +1570,9 @@ variadic tuple must end with a list type. Examples: >
var myTuple: tuple<...list<bool>> = ()
<
*vim9-func-declaration* *E1005* *E1007*
-A partial and function can be declared in more or less specific ways:
+ *vim9-partial-declaration*
+ *vim9-func-type*
+A function (or partial) may be declared in the following ways:
func any kind of function reference, no type
checking for arguments or return value
func: void any number and type of arguments, no return
@@ -1567,352 +1604,881 @@ If the return type is "void" the function does not
return a value.
The reference can also be a |Partial|, in which case it stores extra arguments
and/or a dictionary, which are not visible to the caller. Since they are
-called in the same way the declaration is the same.
+called in the same way, the declaration is the same. This interactive example
+prompts for a circle's radius and returns its area to two decimal places,
+using a partial: >vim9
-Custom types can be defined with `:type`: >
- :type MyList list<string>
-Custom types must start with a capital letter, to avoid name clashes with
-builtin types added later, similarly to user functions.
-
-And classes and interfaces can be used as types: >
- :class MyClass
- :var mine: MyClass
+ vim9script
+ def CircleArea(pi: float, radius: float): float
+ return pi * radius->pow(2)
+ enddef
+ const AREA: func(float): float = CircleArea->function([3.14])
+ const RADIUS: float = "Enter a radius value: "->input()->str2float()
+ echo $"
The area of a circle with a radius of {RADIUS} is " ..
+ $"{AREA(RADIUS)} (π to two d.p.)"
+<
+ *vim9-typealias-type*
+Custom types (|typealias|) can be defined with `:type`. They must start with
+a capital letter (which avoids name clashes with either current or future
+builtin types) similar to user functions. This example creates a list of
+perfect squares and reports on |type()| (14, a typealias) and the
|typename()|: >vim9
- :interface MyInterface
- :var mine: MyInterface
+ vim9script
+ type Ln = list<number>
+ final perfect_squares: Ln = [1, 4, 9, 16, 25]
+ echo "Typename (Ln): " ..
+ $"type() is {Ln->type()} and typename() is {Ln->typename()}"
+<
+ *E1105*
+A typealias itself cannot be converted to a string: >vim9
- :class MyTemplate<Targ>
- :var mine: MyTemplate<number>
- :var mine: MyTemplate<string>
+ vim9script
+ type Ln = list<number>
+ const FAILS: func = (): string => {
+ echo $"{Ln}" # E1105: Cannot convert typealias to string
+ }
+<
+ *vim9-class-type* *vim9-interface-type*
+ *vim9-object-type*
+A |class|, |object|, and |interface| may all be used as types. The following
+interactive example prompts for a float value and returns the area of two
+different shapes. It also reports on the |type()| and |typename()| of the
+classes, objects, and interface: >vim9
- :class MyInterface<Targ>
- :var mine: MyInterface<number>
- :var mine: MyInterface<string>
-{not implemented yet}
+ vim9script
+ interface Shape
+ def InfoArea(): tuple<string, float>
+ endinterface
+ class Circle implements Shape
+ var radius: float
+ def InfoArea(): tuple<string, float>
+ return ('Circle (π × r²)', 3.141593 * this.radius->pow(2))
+ enddef
+ endclass
+ class Square implements Shape
+ var side: float
+ def InfoArea(): tuple<string, float>
+ return ('Square (s²)', this.side->pow(2))
+ enddef
+ endclass
+ const INPUT: float = "Enter a float value: "->input()->str2float()
+ echo "
Areas of shapes:"
+ var myCircle: object<Circle> = Circle.new(INPUT)
+ var mySquare: object<Square> = Square.new(INPUT)
+ final shapes: list<Shape> = [myCircle, mySquare]
+ for shape in shapes
+ const [N: string, A: float] = shape.InfoArea()
+ echo $" - {N} has area of {A}"
+ endfor
+ echo "
type() typename()
------ ----------"
+ echo $"Circle {Circle->type()} {Circle->typename()}"
+ echo $"Square {Square->type()} {Square->typename()}"
+ echo $"Shape {Shape->type()} {Shape->typename()}"
+ echo $"MyCircle {myCircle->type()} {myCircle->typename()}"
+ echo $"MySquare {mySquare->type()} {mySquare->typename()}"
+ echo $"shapes {shapes->type()} {shapes->typename()}"
+<
+ *vim9-enum-type* *vim9-enumvalue-type*
+An |enum| may be used as a type (|v:t_enum|). Variables holding enum values
+have the enumvalue type (|v:t_enumvalue|) at runtime. The following
+interactive example prompts for a character and returns information about
+either a square or a rhombus. It also reports on the |type()| and |typename()|
+of the enum and enumvalue: >vim9
+ vim9script
+ enum Quad
+ Square('four', 'only'),
+ Rhombus('opposite', 'no')
+ var eq: string
+ var ra: string
+ def string(): string
+ return $"
A {this.name} has " ..
+ $"{this.eq} equal sides and {this.ra} right angles
"
+ enddef
+ endenum
+ echo "Rhombus (r) or Square (s)?"
+ var myQuad: Quad = getcharstr() =~ '
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/vim_dev/E1vJWPF-001DIR-Aq%40256bit.org.