summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDoug Kearns <dougkearns@gmail.com>2023-12-14 20:26:26 +0100
committerChristian Brabandt <cb@256bit.org>2023-12-14 20:26:26 +0100
commit74da0ee0a24799a312a3a8a65858237185ef7a23 (patch)
treec615199ec9cbbbb5bdea225b1ab9da698f778701
parent549f8c0b4ebe47168c98f46c8b62b1eb33da7c9c (diff)
patch 9.0.2167: Vim9: not consistently using :var for declarationsv9.0.2167
Problem: Vim9-script object/class variable declarations use syntax that is inconsistent with the rest of the language. Solution: Use :var to declare object and class variables. closes: #13670 Signed-off-by: Doug Kearns <dougkearns@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
-rw-r--r--runtime/doc/vim9class.txt101
-rw-r--r--src/errors.h11
-rw-r--r--src/testdir/test_vim9_builtin.vim2
-rw-r--r--src/testdir/test_vim9_class.vim1104
-rw-r--r--src/testdir/test_vim9_disassemble.vim16
-rw-r--r--src/testdir/test_vim9_typealias.vim4
-rw-r--r--src/version.c2
-rw-r--r--src/vim9class.c81
8 files changed, 714 insertions, 607 deletions
diff --git a/runtime/doc/vim9class.txt b/runtime/doc/vim9class.txt
index c0fc7cb037..cb4dd9e217 100644
--- a/runtime/doc/vim9class.txt
+++ b/runtime/doc/vim9class.txt
@@ -78,8 +78,8 @@ Let's start with a simple example: a class that stores a text position (see
below for how to do this more efficiently): >
class TextPosition
- this.lnum: number
- this.col: number
+ var lnum: number
+ var col: number
def new(lnum: number, col: number)
this.lnum = lnum
@@ -156,8 +156,8 @@ On the other hand, if you do not want the object variables to be read directly
from outside the class or its sub-classes, you can make them protected. This
is done by prefixing an underscore to the name: >
- this._lnum: number
- this._col number
+ var _lnum: number
+ var _col number
Now you need to provide methods to get the value of the protected variables.
These are commonly called getters. We recommend using a name that starts with
@@ -209,8 +209,8 @@ Many constructors take values for the object variables. Thus you very often
see this pattern: >
class SomeClass
- this.lnum: number
- this.col: number
+ var lnum: number
+ var col: number
def new(lnum: number, col: number)
this.lnum = lnum
@@ -235,8 +235,8 @@ Putting together this way of using new() and making the variables public
results in a much shorter class definition than what we started with: >
class TextPosition
- public this.lnum: number
- public this.col: number
+ public var lnum: number
+ public var col: number
def new(this.lnum, this.col)
enddef
@@ -277,8 +277,8 @@ Class members are declared with "static". They are used by the name without a
prefix in the class where they are defined: >
class OtherThing
- this.size: number
- static totalSize: number
+ var size: number
+ static var totalSize: number
def new(this.size)
totalSize += this.size
@@ -297,9 +297,9 @@ underscore as the first character in the name, and it can be made public by
prefixing "public": >
class OtherThing
- static total: number # anybody can read, only class can write
- static _sum: number # only class can read and write
- public static result: number # anybody can read and write
+ static var total: number # anybody can read, only class can write
+ static var _sum: number # only class can read and write
+ public static var result: number # anybody can read and write
endclass
<
*class-method*
@@ -308,8 +308,8 @@ variables but they have no access to the object variables, they cannot use the
"this" keyword:
>
class OtherThing
- this.size: number
- static totalSize: number
+ var size: number
+ static var totalSize: number
# Clear the total size and return the value it had before.
static def ClearTotalSize(): number
@@ -345,14 +345,14 @@ outside of the defining class: >
vim9script
class Vehicle
- static nextID: number = 1000
+ static var nextID: number = 1000
static def GetID(): number
nextID += 1
return nextID
enddef
endclass
class Car extends Vehicle
- this.myID: number
+ var myID: number
def new()
this.myID = Vehicle.GetID()
enddef
@@ -380,20 +380,20 @@ it is. The Shape class functions as the base for a Square and a Triangle
class, for which objects can be created. Example: >
abstract class Shape
- this.color = Color.Black
- this.thickness = 10
+ var color = Color.Black
+ var thickness = 10
endclass
class Square extends Shape
- this.size: number
+ var size: number
def new(this.size)
enddef
endclass
class Triangle extends Shape
- this.base: number
- this.height: number
+ var base: number
+ var height: number
def new(this.base, this.height)
enddef
@@ -430,8 +430,8 @@ interface called HasSurface, which specifies one method Surface() that returns
a number. This example extends the one above: >
abstract class Shape
- this.color = Color.Black
- this.thickness = 10
+ var color = Color.Black
+ var thickness = 10
endclass
interface HasSurface
@@ -439,7 +439,7 @@ a number. This example extends the one above: >
endinterface
class Square extends Shape implements HasSurface
- this.size: number
+ var size: number
def new(this.size)
enddef
@@ -450,8 +450,8 @@ a number. This example extends the one above: >
endclass
class Triangle extends Shape implements HasSurface
- this.base: number
- this.height: number
+ var base: number
+ var height: number
def new(this.base, this.height)
enddef
@@ -598,13 +598,13 @@ Items in a class ~
*E1318* *E1325* *E1388*
Inside a class, in between `:class` and `:endclass`, these items can appear:
- An object variable declaration: >
- this._protectedVariableName: memberType
- this.readonlyVariableName: memberType
- public this.readwriteVariableName: memberType
+ var _protectedVariableName: memberType
+ var readonlyVariableName: memberType
+ public var readwriteVariableName: memberType
- A class variable declaration: >
- static _protectedClassVariableName: memberType
- static readonlyClassVariableName: memberType
- static public readwriteClassVariableName: memberType
+ static var _protectedClassVariableName: memberType
+ static var readonlyClassVariableName: memberType
+ static var public readwriteClassVariableName: memberType
- A constructor method: >
def new(arguments)
def newName(arguments)
@@ -620,9 +620,9 @@ this explicitly with ": {type}". For simple types you can also use an
initializer, such as "= 123", and Vim will see that the type is a number.
Avoid doing this for more complex types and when the type will be incomplete.
For example: >
- this.nameList = []
+ var nameList = []
This specifies a list, but the item type is unknown. Better use: >
- this.nameList: list<string>
+ var nameList: list<string>
The initialization isn't needed, the list is empty by default.
*E1330*
Some types cannot be used, such as "void", "null" and "v:none".
@@ -646,7 +646,7 @@ An interface can declare methods with `:def`, including the arguments and
return type, but without the body and without `:enddef`. Example: >
interface HasSurface
- this.size: number
+ var size: number
def Surface(): number
endinterface
@@ -674,9 +674,9 @@ defined. This default constructor will have arguments for all the object
variables, in the order they were specified. Thus if your class looks like: >
class AutoNew
- this.name: string
- this.age: number
- this.gender: Gender
+ var name: string
+ var age: number
+ var gender: Gender
endclass
Then the default constructor will be: >
@@ -690,8 +690,8 @@ value for the object variables will be used. This is a more useful example,
with default values: >
class TextPosition
- this.lnum: number = 1
- this.col: number = 1
+ var lnum: number = 1
+ var col: number = 1
endclass
If you want the constructor to have mandatory arguments, you need to write it
@@ -947,26 +947,26 @@ Following that Vim object variables could be declared like this: >
Some users pointed out that this looks more like an assignment than a
declaration. Adding "var" changes that: >
class Point
- var this.x: number
- var this.y = 0
+ var x: number
+ var y = 0
endclass
We also need to be able to declare class variables using the "static" keyword.
There we can also choose to leave out "var": >
class Point
- var this.x: number
+ var x: number
static count = 0
endclass
Or do use it, before "static": >
class Point
- var this.x: number
+ var x: number
var static count = 0
endclass
Or after "static": >
class Point
- var this.x: number
+ var x: number
static var count = 0
endclass
@@ -974,9 +974,16 @@ This is more in line with "static def Func()".
There is no clear preference whether to use "var" or not. The two main
reasons to leave it out are:
-1. TypeScript, Java and other popular languages do not use it.
+1. TypeScript and other popular languages do not use it.
2. Less clutter.
+However, it is more common for languages to reuse their general variable and
+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 ~
diff --git a/src/errors.h b/src/errors.h
index 7192514f0e..b6abf4a1d8 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -3402,11 +3402,12 @@ EXTERN char e_object_required_found_str[]
INIT(= N_("E1327: Object required, found %s"));
EXTERN char e_constructor_default_value_must_be_vnone_str[]
INIT(= N_("E1328: Constructor default value must be v:none: %s"));
-// E1329 unused
+EXTERN char e_invalid_class_variable_declaration_str[]
+ INIT(= N_("E1329: Invalid class variable declaration: %s"));
EXTERN char e_invalid_type_for_object_variable_str[]
INIT(= N_("E1330: Invalid type for object variable: %s"));
-EXTERN char e_public_must_be_followed_by_this_or_static[]
- INIT(= N_("E1331: Public must be followed by \"this\" or \"static\""));
+EXTERN char e_public_must_be_followed_by_var_or_static[]
+ INIT(= N_("E1331: Public must be followed by \"var\" or \"static\""));
EXTERN char e_public_variable_name_cannot_start_with_underscore_str[]
INIT(= N_("E1332: Public variable name cannot start with underscore: %s"));
EXTERN char e_cannot_access_protected_variable_str[]
@@ -3487,8 +3488,8 @@ EXTERN char e_cannot_access_protected_method_str[]
INIT(= N_("E1366: Cannot access protected method: %s"));
EXTERN char e_variable_str_of_interface_str_has_different_access[]
INIT(= N_("E1367: Access level of variable \"%s\" of interface \"%s\" is different"));
-EXTERN char e_static_cannot_be_followed_by_this[]
- INIT(= N_("E1368: Static cannot be followed by \"this\" in a variable name"));
+EXTERN char e_static_must_be_followed_by_var_or_def[]
+ INIT(= N_("E1368: Static must be followed by \"var\" or \"def\""));
EXTERN char e_duplicate_variable_str[]
INIT(= N_("E1369: Duplicate variable: %s"));
EXTERN char e_cannot_define_new_method_as_static[]
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index 216118ec68..1267936885 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -4845,7 +4845,7 @@ def Test_values()
vim9script
class Foo
- this.val: number
+ var val: number
def Add()
echo this.val
enddef
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim
index 6f9723dfae..bb806cce91 100644
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -67,6 +67,33 @@ def Test_class_basic()
END
v9.CheckSourceFailure(lines, "E488: Trailing characters: | echo 'done'", 3)
+ # Use old "this." prefixed member variable declaration syntax (without intialization)
+ lines =<< trim END
+ vim9script
+ class Something
+ this.count: number
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this.count: number', 3)
+
+ # Use old "this." prefixed member variable declaration syntax (with intialization)
+ lines =<< trim END
+ vim9script
+ class Something
+ this.count: number = 42
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this.count: number = 42', 3)
+
+ # Use old "this." prefixed member variable declaration syntax (type inferred)
+ lines =<< trim END
+ vim9script
+ class Something
+ this.count = 42
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this.count = 42', 3)
+
# Use "this" without any member variable name
lines =<< trim END
vim9script
@@ -74,7 +101,7 @@ def Test_class_basic()
this
endclass
END
- v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this', 3)
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this', 3)
# Use "this." without any member variable name
lines =<< trim END
@@ -83,7 +110,7 @@ def Test_class_basic()
this.
endclass
END
- v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this.', 3)
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this.', 3)
# Space between "this" and ".<variable>"
lines =<< trim END
@@ -92,7 +119,7 @@ def Test_class_basic()
this .count
endclass
END
- v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this .count', 3)
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this .count', 3)
# Space between "this." and the member variable name
lines =<< trim END
@@ -101,26 +128,44 @@ def Test_class_basic()
this. count
endclass
END
- v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this. count', 3)
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this. count', 3)
# Use "that" instead of "this"
lines =<< trim END
vim9script
class Something
- this.count: number
+ var count: number
that.count
endclass
END
v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: that.count', 4)
- # Member variable without a type or initialization
+ # Use "variable" instead of "var" for member variable declaration (without initialization)
lines =<< trim END
vim9script
class Something
- this.count
+ variable count: number
endclass
END
- v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3)
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: variable count: number', 3)
+
+ # Use "variable" instead of "var" for member variable declaration (with initialization)
+ lines =<< trim END
+ vim9script
+ class Something
+ variable count: number = 42
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: variable count: number = 42', 3)
+
+ # Use "variable" instead of "var" for member variable declaration (type inferred)
+ lines =<< trim END
+ vim9script
+ class Something
+ variable count = 42
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: variable count = 42', 3)
# Use a non-existing member variable in new()
lines =<< trim END
@@ -138,7 +183,7 @@ def Test_class_basic()
lines =<< trim END
vim9script
class Something
- this.count : number
+ var count : number
endclass
END
v9.CheckSourceFailure(lines, 'E1059: No white space allowed before colon: count : number', 3)
@@ -147,11 +192,38 @@ def Test_class_basic()
lines =<< trim END
vim9script
class Something
- this.count:number
+ var count:number
endclass
END
v9.CheckSourceFailure(lines, "E1069: White space required after ':'", 3)
+ # Missing ":var" in a "var" member variable declaration (without initialization)
+ lines =<< trim END
+ vim9script
+ class Something
+ var: number
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: var: number', 3)
+
+ # Missing ":var" in a "var" member variable declaration (with initialization)
+ lines =<< trim END
+ vim9script
+ class Something
+ var: number = 42
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: var: number = 42', 3)
+
+ # Missing ":var" in a "var" member variable declaration (type inferred)
+ lines =<< trim END
+ vim9script
+ class Something
+ var = 42
+ endclass
+ END
+ v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: var = 42', 3)
+
# Test for unsupported comment specifier
lines =<< trim END
vim9script
@@ -227,8 +299,8 @@ def Test_class_basic()
vim9script
class TextPosition
- this.lnum: number
- this.col: number
+ var lnum: number
+ var col: number
# make a nicely formatted string
def ToString(): string
@@ -293,7 +365,7 @@ def Test_class_basic()
lines =<< trim END
vim9script
class A
- this.y = {
+ var y = {
X: 1
}
endclass
@@ -312,7 +384,7 @@ def Test_class_def_method()
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "this" or "static"', 3)
+ v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
# Using the "public" keyword when defining a class method
lines =<< trim END
@@ -332,7 +404,7 @@ def Test_class_def_method()
enddef
endclass
END
- v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "this" or "static"', 3)
+ v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
# Using the "public" keyword when defining a class protected method
lines =<< trim END
@@ -415,7 +487,7 @@ def Test_class_interface_wrong_end()
var lines =<< trim END
vim9script
abstract class SomeName
- this.member = 'text'
+ var member = 'text'
endinterface
END
v9.CheckSourceFailure(lines, 'E476: Invalid command: endinterface, expected endclass', 4)
@@ -423,7 +495,7 @@ def Test_class_interface_wrong_end()
lines =<< trim END
vim9script
export interface AnotherName
- this.member: string
+ var member: string
endclass
END
v9.CheckSourceFailure(lines, 'E476: Invalid command: endclass, expected endinterface', 4)
@@ -435,7 +507,7 @@ def Test_object_not_set()
vim9script
class State
- this.value = 'xyz'
+ var value = 'xyz'
endclass
var state: State
@@ -449,7 +521,7 @@ def Test_object_not_set()
vim9script
class Class
- this.id: string
+ var id: string
def Method1()
echo 'Method1' .. this.id
enddef
@@ -469,11 +541,11 @@ def Test_object_not_set()
vim9script
class Background
- this.background = 'dark'
+ var background = 'dark'
endclass
class Colorscheme
- this._bg: Background
+ var _bg: Background
def GetBackground(): string
return this._bg.background
@@ -490,7 +562,7 @@ def Test_object_not_set()
vim9script
class Class
- this.id: string
+ var id: string
def Method1()
echo 'Method1' .. this.id
enddef
@@ -552,8 +624,8 @@ def Test_class_member_initializer()
vim9script
class TextPosition
- this.lnum: number = 1
- this.col: number = 1
+ var lnum: number = 1
+ var col: number = 1
# constructor with only the line number
def new(lnum: number)
@@ -588,11 +660,11 @@ def Test_member_any_used_as_object()
vim9script
class Inner
- this.value: number = 0
+ var value: number = 0
endclass
class Outer
- this.inner: any
+ var inner: any
endclass
def F(outer: Outer)
@@ -611,11 +683,11 @@ def Test_member_any_used_as_object()
vim9script
class Inner
- this._value: string = ''
+ var _value: string = ''
endclass
class Outer
- this.inner: any
+ var inner: any
endclass
def F(outer: Outer)
@@ -633,11 +705,11 @@ def Test_member_any_used_as_object()
vim9script
class Inner
- this.value: string = ''
+ var value: string = ''
endclass
class Outer
- this.inner: any
+ var inner: any
endclass
def F(outer: Outer)
@@ -657,11 +729,11 @@ def Test_assignment_nested_type()
vim9script
class Inner
- public this.value: number = 0
+ public var value: number = 0
endclass
class Outer
- this.inner: Inner
+ var inner: Inner
endclass
def F(outer: Outer)
@@ -689,11 +761,11 @@ def Test_assignment_nested_type()
vim9script
class Inner
- this.value: number = 0
+ var value: number = 0
endclass
class Outer
- this.inner: Inner
+ var inner: Inner
endclass
def F(outer: Outer)
@@ -716,11 +788,11 @@ def Test_assignment_nested_type()
vim9script
class Inner
- this.value: number = 0
+ var value: number = 0
endclass
class Outer
- this.inner: Inner
+ var inner: Inner
endclass
def F(outer: Outer)
@@ -741,7 +813,7 @@ def Test_assignment_with_operator()
vim9script
class Foo
- public this.x: number
+ public var x: number
def Add(n: number)
this.x += n
@@ -788,7 +860,7 @@ def Test_expr_after_using_object()
vim9script
class Something
- this.label: string = ''
+ var label: string = ''
endclass
def Foo(): Something
@@ -807,8 +879,8 @@ def Test_class_default_new()
vim9script
class TextPosition
- this.lnum: number = 1
- this.col: number = 1
+ var lnum: number = 1
+ var col: number = 1
endclass
var pos = TextPosition.new()
@@ -832,9 +904,9 @@ def Test_class_default_new()
lines =<< trim END
vim9script
class Person
- this.name: string
- this.age: number = 42
- this.education: string = "unknown"
+ var name: string
+ var age: number = 42
+ var education: string = "unknown"
def new(this.name, this.age = v:none, this.education = v:none)
enddef
@@ -855,9 +927,9 @@ def Test_class_default_new()
lines =<< trim END
vim9script
class Person
- this.name: string
- this.age: number = 42
- this.education: string = "unknown"
+ var name: string
+ var age: number = 42
+ var education: string = "unknown"
def new(this.name, this.age = v:none, this.education = v:none)
enddef
@@ -872,7 +944,7 @@ def Test_class_default_new()
lines =<< trim END
vim9script
class A
- this.val: string
+ var val: string
def new(this.val = 'a')
enddef
endclass
@@ -885,8 +957,8 @@ def Test_class_new_with_object_member()
vim9script
class C
- this.str: string
- this.num: number
+ var str: string
+ var num: number
def new(this.str, this.num)
enddef
def newVals(this.str, this.num)
@@ -915,8 +987,8 @@ def Test_class_new_with_object_member()
vim9script
class C
- this.str: string
- this.num: number
+ var str: string
+ var num: number
def new(this.str, this.num)
enddef
endclass
@@ -937,8 +1009,8 @@ def Test_class_new_with_object_member()
vim9script
class C
- this.str: string
- this.num: number
+ var str: string
+ var num: number
def newVals(this.str, this.num)
enddef
endclass
@@ -959,7 +1031,7 @@ def Test_class_new_with_object_member()
vim9script
class C
- this.str: string
+ var str: string
def new(str: any)
enddef
endclass
@@ -980,7 +1052,7 @@ def Test_class_new_with_object_member()
lines =<< trim END
vim9script
class A
- this.val = 10
+ var val = 10
static def Foo(this.val: number)
enddef
endclass
@@ -991,7 +1063,7 @@ def Test_class_new_with_object_member()
lines =<< trim END
vim9script
class A
- this.val = 10
+ var val = 10
def Foo(this.val: number)
enddef
endclass
@@ -1003,9 +1075,9 @@ def Test_class_object_member_inits()
var lines =<< trim END
vim9script
class TextPosition
- this.lnum: number
- this.col = 1
- this.addcol: number = 2
+ var lnum: number
+ var col = 1
+ var addcol: number = 2
endclass
var pos = TextPosition.new()
@@ -1018,8 +1090,8 @@ def Test_class_object_member_inits()
lines =<< trim END
vim9script
class TextPosition
- this.lnum
- this.col = 1
+ var lnum
+ var col = 1
endclass
END
v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3)
@@ -1036,9 +1108,9 @@ def Test_class_object_member_inits()
enddef
class A
- this.str1 = Init()
- this.str2: string = Init()
- this.col = 1
+ var str1 = Init()
+ var str2: string = Init()
+ var col = 1
endclass
assert_equal(init_count, 0)
@@ -1051,7 +1123,7 @@ def Test_class_object_member_inits()
lines =<< trim END
vim9script
class A
- this.value = init_val
+ var value = init_val
endclass
var a = A.new()
END
@@ -1061,7 +1133,7 @@ def Test_class_object_member_inits()
lines =<< trim END
vim9script
class A
- this.value: void
+ var value: void
endclass
END
v9.CheckSourceFailure(lines, 'E1330: Invalid type for object variable: void', 3)
@@ -1072,9 +1144,9 @@ def Test_instance_variable_access()
var lines =<< trim END
vim9script
class Triple
- this._one = 1
- this.two = 2
- public this.three = 3
+ var _one = 1
+ var two = 2
+ public var three = 3
def GetOne(): number
return this._one
@@ -1100,17 +1172,17 @@ def Test_instance_variable_access()
lines =<< trim END
vim9script
class A
- public this._val = 10
+ public var _val = 10
endclass
END
- v9.CheckSourceFailure(lines, 'E1332: Public variable name cannot start with underscore: public this._val = 10', 3)
+ v9.CheckSourceFailure(lines, 'E1332: Public variable name cannot start with underscore: public var _val = 10', 3)
lines =<< trim END
vim9script
class MyCar
- this.make: string
- this.age = 5
+ var make: string
+ var age = 5
def new(make_arg: string)
this.make = make_arg
@@ -1145,7 +1217,7 @@ def Test_instance_variable_access()
vim9script
class MyCar
- this.make: string
+ var make: string
def new(make_arg: string)
this.make = make_arg
@@ -1161,7 +1233,7 @@ def Test_instance_variable_access()
vim9script
class Foo
- this.x: list<number> = []
+ var x: list<number> = []
def Add(n: number): any
this.x->add(n)
@@ -1187,25 +1259,25 @@ def Test_instance_variable_access()
lines =<< trim END
vim9script
class Something
- pub this.val = 1
+ pub var val = 1
endclass
END
- v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: pub this.val = 1', 3)
+ v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: pub var val = 1', 3)
- # Test for "public" keyword must be followed by "this" or "static".
+ # Test for "public" keyword must be followed by "var" or "static".
lines =<< trim END
vim9script
class Something
public val = 1
endclass
END
- v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "this" or "static"', 3)
+ v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
# Modify a instance variable using the class name in the script context
lines =<< trim END
vim9script
class A
- public this.val = 1
+ public var val = 1
endclass
A.val = 1
END
@@ -1215,7 +1287,7 @@ def Test_instance_variable_access()
lines =<< trim END
vim9script
class A
- public this.val = 1
+ public var val = 1
endclass
var i = A.val
END
@@ -1225,7 +1297,7 @@ def Test_instance_variable_access()
lines =<< trim END
vim9script
class A
- public this.val = 1
+ public var val = 1
endclass
def T()
A.val = 1
@@ -1238,7 +1310,7 @@ def Test_instance_variable_access()
lines =<< trim END
vim9script
class A
- public this.val = 1
+ public var val = 1
endclass
def T()
var i = A.val
@@ -1251,9 +1323,9 @@ def Test_instance_variable_access()
lines =<< trim END
vim9script
class A
- this.ro_obj_var = 10
- public this.rw_obj_var = 20
- this._priv_obj_var = 30
+ var ro_obj_var = 10
+ public var rw_obj_var = 20
+ var _priv_obj_var = 30
endclass
class B extends A
@@ -1280,34 +1352,25 @@ def Test_class_variable_access()
var lines =<< trim END
vim9script
class Something
- stat this.val = 1
+ stat var val = 1
endclass
END
- v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: stat this.val = 1', 3)
-
- # Test for "static" cannot be followed by "this".
- lines =<< trim END
- vim9script
- class Something
- static this.val = 1
- endclass
- END
- v9.CheckSourceFailure(lines, 'E1368: Static cannot be followed by "this" in a variable name', 3)
+ v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: stat var val = 1', 3)
# Test for "static" cannot be followed by "public".
lines =<< trim END
vim9script
class Something
- static public val = 1
+ static public var val = 1
endclass
END
- v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3)
+ v9.CheckSourceFailure(lines, 'E1368: Static must be followed by "var" or "def"', 3)
# A readonly class variable cannot be modified from a child class
lines =<< trim END
vim9script
class A
- static ro_class_var = 40
+ static var ro_class_var = 40
endclass
class B extends A
@@ -1325,7 +1388,7 @@ def Test_class_variable_access()
lines =<< trim END
vim9script
class A
- static _priv_class_var = 60
+ static var _priv_class_var = 60
endclass
class B extends A
@@ -1343,7 +1406,7 @@ def Test_class_variable_access()
lines =<< trim END
vim9script
class A
- static _priv_class_var = 60
+ static var _priv_class_var = 60
endclass
class B extends A
@@ -1361,9 +1424,9 @@ def Test_class_variable_access()
lines =<< trim END
vim9script
class A
- static ro_class_var = 10
- public static rw_class_var = 20
- static _priv_class_var = 30
+ static var ro_class_var = 10
+ public static var rw_class_var = 20
+ static var _priv_class_var = 30
endclass
class B extends A
@@ -1392,8 +1455,8 @@ def Test_class_object_compare()
var class_lines =<< trim END
vim9script
class Item
- this.nr = 0
- this.name = 'xx'
+ var nr = 0
+ var name = 'xx'
endclass
END
@@ -1435,13 +1498,13 @@ def Test_object_type()
vim9script
class One
- this.one = 1
+ var one = 1
endclass
class Two
- this.two = 2
+ var two = 2
endclass
class TwoMore extends Two
- this.more = 9
+ var more = 9
endclass
var o: One = One.new()
@@ -1457,10 +1520,10 @@ def Test_object_type()
vim9script
class One
- this.one = 1
+ var one = 1
endclass
class Two
- this.two = 2
+ var two = 2
endclass
var o: One = Two.new()
@@ -1474,7 +1537,7 @@ def Test_object_type()
def GetMember(): number
endinterface
class Two implements One
- this.one = 1
+ var one = 1
def GetMember(): number
return this.one
enddef