patch 9.0.2170: Vim9: no support for const/final class/objects vars
Problem: Vim9: no support for const/final class/objects vars
Solution: Support final and const object and class variables
closes: #13655
Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/runtime/doc/tags b/runtime/doc/tags
index 964789c..0b9cdff 100644
--- a/runtime/doc/tags
+++ b/runtime/doc/tags
@@ -4505,7 +4505,10 @@
E1402 vim9class.txt /*E1402*
E1403 vim9class.txt /*E1403*
E1407 vim9class.txt /*E1407*
+E1408 vim9class.txt /*E1408*
+E1409 vim9class.txt /*E1409*
E141 message.txt /*E141*
+E1410 vim9class.txt /*E1410*
E142 message.txt /*E142*
E143 autocmd.txt /*E143*
E144 various.txt /*E144*
@@ -9096,6 +9099,8 @@
o_object-select motion.txt /*o_object-select*
o_v motion.txt /*o_v*
object vim9class.txt /*object*
+object-const-variable vim9class.txt /*object-const-variable*
+object-final-variable vim9class.txt /*object-final-variable*
object-motions motion.txt /*object-motions*
object-select motion.txt /*object-select*
objects index.txt /*objects*
diff --git a/runtime/doc/todo.txt b/runtime/doc/todo.txt
index 5e844a0..afe5aed 100644
--- a/runtime/doc/todo.txt
+++ b/runtime/doc/todo.txt
@@ -1,4 +1,4 @@
-*todo.txt* For Vim version 9.0. Last change: 2023 Jun 08
+*todo.txt* For Vim version 9.0. Last change: 2023 Dec 14
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -122,7 +122,6 @@
Further Vim9 improvements:
- Classes and Interfaces. See |vim9-classes|
- - "final" object members - can only be set in the constructor.
- Cannot use class type of itself in the method (Issue #12369)
- Getting member of variable with "any" type should be handled at runtime.
Remove temporary solution from #12096 / patch 9.0.1375.
diff --git a/runtime/doc/vim9class.txt b/runtime/doc/vim9class.txt
index 97813f0..4dc67bd 100644
--- a/runtime/doc/vim9class.txt
+++ b/runtime/doc/vim9class.txt
@@ -364,6 +364,78 @@
corresponding class member will be used. The type of the class member in a
child class can be different from that in the super class.
+ *object-final-variable* *E1409*
+The |:final| keyword can be used to make a class or object variable a
+constant. Examples: >
+
+ class A
+ final v1 = [1, 2] # final object variable
+ public final v2 = {x: 1} # final object variable
+ static final v3 = 'abc' # final class variable
+ public static final v4 = 0z10 # final class variable
+ endclass
+<
+A final variable can be changed only from a constructor function. Example: >
+
+ class A
+ final v1: list<number>
+ def new()
+ this.v1 = [1, 2]
+ enddef
+ endclass
+ var a = A.new()
+ echo a.v1
+<
+Note that the value of a final variable can be changed. Example: >
+
+ class A
+ public final v1 = [1, 2]
+ endclass
+ var a = A.new()
+ a.v1[0] = 6 # OK
+ a.v1->add(3) # OK
+ a.v1 = [3, 4] # Error
+<
+ *E1408*
+Final variables are not supported in an interface. A class or object method
+cannot be final.
+
+ *object-const-variable*
+The |:const| keyword can be used to make a class or object variable and the
+value a constant. Examples: >
+
+ class A
+ const v1 = [1, 2] # const object variable
+ public const v2 = {x: 1} # const object variable
+ static const v3 = 'abc' # const class variable
+ public static const v4 = 0z10 # const class variable
+ endclass
+<
+A const variable can be changed only from a constructor function. Example: >
+
+ class A
+ const v1: list<number>
+ def new()
+ this.v1 = [1, 2]
+ enddef
+ endclass
+ var a = A.new()
+ echo a.v1
+<
+A const variable and its value cannot be changed. Example: >
+
+ class A
+ public const v1 = [1, 2]
+ endclass
+ var a = A.new()
+ a.v1[0] = 6 # Error
+ a.v1->add(3) # Error
+ a.v1 = [3, 4] # Error
+<
+ *E1410*
+Const variables are not supported in an interface. A class or object method
+cannot be a const.
+
==============================================================================
4. Using an abstract class *Vim9-abstract-class*
@@ -982,8 +1054,6 @@
reuses the general function declaration syntax for methods. So, for the sake
of consistency, we require "var" in these declarations.
-This also allows for a natural use of "final" and "const" in the future.
-
Using "ClassName.new()" to construct an object ~