1
0
forked from aniani/vim

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>
This commit is contained in:
Yegappan Lakshmanan
2023-12-16 14:11:19 +01:00
committed by Christian Brabandt
parent d8bf87c9fb
commit e5437c5427
12 changed files with 834 additions and 21 deletions

View File

@@ -4505,7 +4505,10 @@ E1401 vim9class.txt /*E1401*
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_V motion.txt /*o_V*
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*

View File

@@ -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 @@ Upcoming larger works:
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.

View File

@@ -364,6 +364,78 @@ super class. Depending on the class where the member is used the
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 @@ function declaration syntax for class/object variables and methods. Vim9 also
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 ~