comparison src/testdir/test_vim9_class.vim @ 33942:3bba09502b8d v9.0.2167

patch 9.0.2167: Vim9: not consistently using :var for declarations Commit: https://github.com/vim/vim/commit/74da0ee0a24799a312a3a8a65858237185ef7a23 Author: Doug Kearns <dougkearns@gmail.com> Date: Thu Dec 14 20:26:26 2023 +0100 patch 9.0.2167: Vim9: not consistently using :var for declarations 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>
author Christian Brabandt <cb@256bit.org>
date Thu, 14 Dec 2023 20:30:06 +0100
parents bdd408288d95
children 45a50fd59a73
comparison
equal deleted inserted replaced
33941:8845f55a6c20 33942:3bba09502b8d
65 class Something 65 class Something
66 endclass | echo 'done' 66 endclass | echo 'done'
67 END 67 END
68 v9.CheckSourceFailure(lines, "E488: Trailing characters: | echo 'done'", 3) 68 v9.CheckSourceFailure(lines, "E488: Trailing characters: | echo 'done'", 3)
69 69
70 # Use old "this." prefixed member variable declaration syntax (without intialization)
71 lines =<< trim END
72 vim9script
73 class Something
74 this.count: number
75 endclass
76 END
77 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this.count: number', 3)
78
79 # Use old "this." prefixed member variable declaration syntax (with intialization)
80 lines =<< trim END
81 vim9script
82 class Something
83 this.count: number = 42
84 endclass
85 END
86 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this.count: number = 42', 3)
87
88 # Use old "this." prefixed member variable declaration syntax (type inferred)
89 lines =<< trim END
90 vim9script
91 class Something
92 this.count = 42
93 endclass
94 END
95 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this.count = 42', 3)
96
70 # Use "this" without any member variable name 97 # Use "this" without any member variable name
71 lines =<< trim END 98 lines =<< trim END
72 vim9script 99 vim9script
73 class Something 100 class Something
74 this 101 this
75 endclass 102 endclass
76 END 103 END
77 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this', 3) 104 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this', 3)
78 105
79 # Use "this." without any member variable name 106 # Use "this." without any member variable name
80 lines =<< trim END 107 lines =<< trim END
81 vim9script 108 vim9script
82 class Something 109 class Something
83 this. 110 this.
84 endclass 111 endclass
85 END 112 END
86 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this.', 3) 113 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this.', 3)
87 114
88 # Space between "this" and ".<variable>" 115 # Space between "this" and ".<variable>"
89 lines =<< trim END 116 lines =<< trim END
90 vim9script 117 vim9script
91 class Something 118 class Something
92 this .count 119 this .count
93 endclass 120 endclass
94 END 121 END
95 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this .count', 3) 122 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this .count', 3)
96 123
97 # Space between "this." and the member variable name 124 # Space between "this." and the member variable name
98 lines =<< trim END 125 lines =<< trim END
99 vim9script 126 vim9script
100 class Something 127 class Something
101 this. count 128 this. count
102 endclass 129 endclass
103 END 130 END
104 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this. count', 3) 131 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this. count', 3)
105 132
106 # Use "that" instead of "this" 133 # Use "that" instead of "this"
107 lines =<< trim END 134 lines =<< trim END
108 vim9script 135 vim9script
109 class Something 136 class Something
110 this.count: number 137 var count: number
111 that.count 138 that.count
112 endclass 139 endclass
113 END 140 END
114 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: that.count', 4) 141 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: that.count', 4)
115 142
116 # Member variable without a type or initialization 143 # Use "variable" instead of "var" for member variable declaration (without initialization)
117 lines =<< trim END 144 lines =<< trim END
118 vim9script 145 vim9script
119 class Something 146 class Something
120 this.count 147 variable count: number
121 endclass 148 endclass
122 END 149 END
123 v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3) 150 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: variable count: number', 3)
151
152 # Use "variable" instead of "var" for member variable declaration (with initialization)
153 lines =<< trim END
154 vim9script
155 class Something
156 variable count: number = 42
157 endclass
158 END
159 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: variable count: number = 42', 3)
160
161 # Use "variable" instead of "var" for member variable declaration (type inferred)
162 lines =<< trim END
163 vim9script
164 class Something
165 variable count = 42
166 endclass
167 END
168 v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: variable count = 42', 3)
124 169
125 # Use a non-existing member variable in new() 170 # Use a non-existing member variable in new()
126 lines =<< trim END 171 lines =<< trim END
127 vim9script 172 vim9script
128 class Something 173 class Something
136 181
137 # Space before ":" in a member variable declaration 182 # Space before ":" in a member variable declaration
138 lines =<< trim END 183 lines =<< trim END
139 vim9script 184 vim9script
140 class Something 185 class Something
141 this.count : number 186 var count : number
142 endclass 187 endclass
143 END 188 END
144 v9.CheckSourceFailure(lines, 'E1059: No white space allowed before colon: count : number', 3) 189 v9.CheckSourceFailure(lines, 'E1059: No white space allowed before colon: count : number', 3)
145 190
146 # No space after ":" in a member variable declaration 191 # No space after ":" in a member variable declaration
147 lines =<< trim END 192 lines =<< trim END
148 vim9script 193 vim9script
149 class Something 194 class Something
150 this.count:number 195 var count:number
151 endclass 196 endclass
152 END 197 END
153 v9.CheckSourceFailure(lines, "E1069: White space required after ':'", 3) 198 v9.CheckSourceFailure(lines, "E1069: White space required after ':'", 3)
199
200 # Missing ":var" in a "var" member variable declaration (without initialization)
201 lines =<< trim END
202 vim9script
203 class Something
204 var: number
205 endclass
206 END
207 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: var: number', 3)
208
209 # Missing ":var" in a "var" member variable declaration (with initialization)
210 lines =<< trim END
211 vim9script
212 class Something
213 var: number = 42
214 endclass
215 END
216 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: var: number = 42', 3)
217
218 # Missing ":var" in a "var" member variable declaration (type inferred)
219 lines =<< trim END
220 vim9script
221 class Something
222 var = 42
223 endclass
224 END
225 v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: var = 42', 3)
154 226
155 # Test for unsupported comment specifier 227 # Test for unsupported comment specifier
156 lines =<< trim END 228 lines =<< trim END
157 vim9script 229 vim9script
158 class Something 230 class Something
225 # method. Check for using type() and typename() with a class and an object. 297 # method. Check for using type() and typename() with a class and an object.
226 lines =<< trim END 298 lines =<< trim END
227 vim9script 299 vim9script
228 300
229 class TextPosition 301 class TextPosition
230 this.lnum: number 302 var lnum: number
231 this.col: number 303 var col: number
232 304
233 # make a nicely formatted string 305 # make a nicely formatted string
234 def ToString(): string 306 def ToString(): string
235 return $'({this.lnum}, {this.col})' 307 return $'({this.lnum}, {this.col})'
236 enddef 308 enddef
291 363
292 # Use a multi-line initialization for a member variable 364 # Use a multi-line initialization for a member variable
293 lines =<< trim END 365 lines =<< trim END
294 vim9script 366 vim9script
295 class A 367 class A
296 this.y = { 368 var y = {
297 X: 1 369 X: 1
298 } 370 }
299 endclass 371 endclass
300 var a = A.new() 372 var a = A.new()
301 END 373 END
310 class A 382 class A
311 public def Foo() 383 public def Foo()
312 enddef 384 enddef
313 endclass 385 endclass
314 END 386 END
315 v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "this" or "static"', 3) 387 v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
316 388
317 # Using the "public" keyword when defining a class method 389 # Using the "public" keyword when defining a class method
318 lines =<< trim END 390 lines =<< trim END
319 vim9script 391 vim9script
320 class A 392 class A
330 class A 402 class A
331 public def _Foo() 403 public def _Foo()
332 enddef 404 enddef
333 endclass 405 endclass
334 END 406 END
335 v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "this" or "static"', 3) 407 v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
336 408
337 # Using the "public" keyword when defining a class protected method 409 # Using the "public" keyword when defining a class protected method
338 lines =<< trim END 410 lines =<< trim END
339 vim9script 411 vim9script
340 class A 412 class A
413 485
414 def Test_class_interface_wrong_end() 486 def Test_class_interface_wrong_end()
415 var lines =<< trim END 487 var lines =<< trim END
416 vim9script 488 vim9script
417 abstract class SomeName 489 abstract class SomeName
418 this.member = 'text' 490 var member = 'text'
419 endinterface 491 endinterface
420 END 492 END
421 v9.CheckSourceFailure(lines, 'E476: Invalid command: endinterface, expected endclass', 4) 493 v9.CheckSourceFailure(lines, 'E476: Invalid command: endinterface, expected endclass', 4)
422 494
423 lines =<< trim END 495 lines =<< trim END
424 vim9script 496 vim9script
425 export interface AnotherName 497 export interface AnotherName
426 this.member: string 498 var member: string
427 endclass 499 endclass
428 END 500 END
429 v9.CheckSourceFailure(lines, 'E476: Invalid command: endclass, expected endinterface', 4) 501 v9.CheckSourceFailure(lines, 'E476: Invalid command: endclass, expected endinterface', 4)
430 enddef 502 enddef
431 503
433 # Use an uninitialized object in script context 505 # Use an uninitialized object in script context
434 var lines =<< trim END 506 var lines =<< trim END
435 vim9script 507 vim9script
436 508
437 class State 509 class State
438 this.value = 'xyz' 510 var value = 'xyz'
439 endclass 511 endclass
440 512
441 var state: State 513 var state: State
442 var db = {'xyz': 789} 514 var db = {'xyz': 789}
443 echo db[state.value] 515 echo db[state.value]
447 # Use an uninitialized object from a def function 519 # Use an uninitialized object from a def function
448 lines =<< trim END 520 lines =<< trim END
449 vim9script 521 vim9script
450 522
451 class Class 523 class Class
452 this.id: string 524 var id: string
453 def Method1() 525 def Method1()
454 echo 'Method1' .. this.id 526 echo 'Method1' .. this.id
455 enddef 527 enddef
456 endclass 528 endclass
457 529
467 # object method. 539 # object method.
468 lines =<< trim END 540 lines =<< trim END
469 vim9script 541 vim9script
470 542
471 class Background 543 class Background
472 this.background = 'dark' 544 var background = 'dark'
473 endclass 545 endclass
474 546
475 class Colorscheme 547 class Colorscheme
476 this._bg: Background 548 var _bg: Background
477 549
478 def GetBackground(): string 550 def GetBackground(): string
479 return this._bg.background 551 return this._bg.background
480 enddef 552 enddef
481 endclass 553 endclass
488 # TODO: this should not give an error but be handled at runtime 560 # TODO: this should not give an error but be handled at runtime
489 lines =<< trim END 561 lines =<< trim END
490 vim9script 562 vim9script
491 563
492 class Class 564 class Class
493 this.id: string 565 var id: string
494 def Method1() 566 def Method1()
495 echo 'Method1' .. this.id 567 echo 'Method1' .. this.id
496 enddef 568 enddef
497 endclass 569 endclass
498 570
550 def Test_class_member_initializer() 622 def Test_class_member_initializer()
551 var lines =<< trim END 623 var lines =<< trim END
552 vim9script 624 vim9script
553 625
554 class TextPosition 626 class TextPosition
555 this.lnum: number = 1 627 var lnum: number = 1
556 this.col: number = 1 628 var col: number = 1
557 629
558 # constructor with only the line number 630 # constructor with only the line number
559 def new(lnum: number) 631 def new(lnum: number)
560 this.lnum = lnum 632 this.lnum = lnum
561 enddef 633 enddef
586 def Test_member_any_used_as_object() 658 def Test_member_any_used_as_object()
587 var lines =<< trim END 659 var lines =<< trim END
588 vim9script 660 vim9script
589 661
590 class Inner 662 class Inner
591 this.value: number = 0 663 var value: number = 0
592 endclass 664 endclass
593 665
594 class Outer 666 class Outer
595 this.inner: any 667 var inner: any
596 endclass 668 endclass
597 669
598 def F(outer: Outer) 670 def F(outer: Outer)
599 outer.inner.value = 1 671 outer.inner.value = 1
600 enddef 672 enddef
609 # Try modifying a protected variable using an "any" object 681 # Try modifying a protected variable using an "any" object
610 lines =<< trim END 682 lines =<< trim END
611 vim9script 683 vim9script
612 684
613 class Inner 685 class Inner
614 this._value: string = '' 686 var _value: string = ''
615 endclass 687 endclass
616 688
617 class Outer 689 class Outer
618 this.inner: any 690 var inner: any
619 endclass 691 endclass
620 692
621 def F(outer: Outer) 693 def F(outer: Outer)
622 outer.inner._value = 'b' 694 outer.inner._value = 'b'
623 enddef 695 enddef
631 # Try modifying a non-existing variable using an "any" object 703 # Try modifying a non-existing variable using an "any" object
632 lines =<< trim END 704 lines =<< trim END
633 vim9script 705 vim9script
634 706
635 class Inner 707 class Inner
636 this.value: string = '' 708 var value: string = ''
637 endclass 709 endclass
638 710
639 class Outer 711 class Outer
640 this.inner: any 712 var inner: any
641 endclass 713 endclass
642 714
643 def F(outer: Outer) 715 def F(outer: Outer)
644 outer.inner.someval = 'b' 716 outer.inner.someval = 'b'
645 enddef 717 enddef
655 def Test_assignment_nested_type() 727 def Test_assignment_nested_type()
656 var lines =<< trim END 728 var lines =<< trim END
657 vim9script 729 vim9script
658 730
659 class Inner 731 class Inner
660 public this.value: number = 0 732 public var value: number = 0
661 endclass 733 endclass
662 734
663 class Outer 735 class Outer
664 this.inner: Inner 736 var inner: Inner
665 endclass 737 endclass
666 738
667 def F(outer: Outer) 739 def F(outer: Outer)
668 outer.inner.value = 1 740 outer.inner.value = 1
669 enddef 741 enddef
687 # Assignment where target item is read only in :def 759 # Assignment where target item is read only in :def
688 lines =<< trim END 760 lines =<< trim END
689 vim9script 761 vim9script
690 762
691 class Inner 763 class Inner
692 this.value: number = 0 764 var value: number = 0
693 endclass 765 endclass
694 766
695 class Outer 767 class Outer
696 this.inner: Inner 768 var inner: Inner
697 endclass 769 endclass
698 770
699 def F(outer: Outer) 771 def F(outer: Outer)
700 outer.inner.value = 1 772 outer.inner.value = 1
701 enddef 773 enddef
714 # Assignment where target item is read only script level 786 # Assignment where target item is read only script level
715 lines =<< trim END 787 lines =<< trim END
716 vim9script 788 vim9script
717 789
718 class Inner 790 class Inner
719 this.value: number = 0 791 var value: number = 0
720 endclass 792 endclass
721 793
722 class Outer 794 class Outer
723 this.inner: Inner 795 var inner: Inner
724 endclass 796 endclass
725 797
726 def F(outer: Outer) 798 def F(outer: Outer)
727 outer.inner.value = 1 799 outer.inner.value = 1
728 enddef 800 enddef
739 # Use "+=" to assign to a object variable 811 # Use "+=" to assign to a object variable
740 var lines =<< trim END 812 var lines =<< trim END
741 vim9script 813 vim9script
742 814
743 class Foo 815 class Foo
744 public this.x: number 816 public var x: number
745 817
746 def Add(n: number) 818 def Add(n: number)
747 this.x += n 819 this.x += n
748 enddef 820 enddef
749 endclass 821 endclass
786 def Test_expr_after_using_object() 858 def Test_expr_after_using_object()
787 var lines =<< trim END 859 var lines =<< trim END
788 vim9script 860 vim9script
789 861
790 class Something 862 class Something
791 this.label: string = '' 863 var label: string = ''
792 endclass 864 endclass
793 865
794 def Foo(): Something 866 def Foo(): Something
795 var v = Something.new() 867 var v = Something.new()
796 echo 'in Foo(): ' .. typename(v) 868 echo 'in Foo(): ' .. typename(v)
805 def Test_class_default_new() 877 def Test_class_default_new()
806 var lines =<< trim END 878 var lines =<< trim END
807 vim9script 879 vim9script
808 880
809 class TextPosition 881 class TextPosition
810 this.lnum: number = 1 882 var lnum: number = 1
811 this.col: number = 1 883 var col: number = 1
812 endclass 884 endclass
813 885
814 var pos = TextPosition.new() 886 var pos = TextPosition.new()
815 assert_equal(1, pos.lnum) 887 assert_equal(1, pos.lnum)
816 assert_equal(1, pos.col) 888 assert_equal(1, pos.col)
830 v9.CheckSourceSuccess(lines) 902 v9.CheckSourceSuccess(lines)
831 903
832 lines =<< trim END 904 lines =<< trim END
833 vim9script 905 vim9script
834 class Person 906 class Person
835 this.name: string 907 var name: string
836 this.age: number = 42 908 var age: number = 42
837 this.education: string = "unknown" 909 var education: string = "unknown"
838 910
839 def new(this.name, this.age = v:none, this.education = v:none) 911 def new(this.name, this.age = v:none, this.education = v:none)
840 enddef 912 enddef
841 endclass 913 endclass
842 914
853 v9.CheckSourceSuccess(lines) 925 v9.CheckSourceSuccess(lines)
854 926
855 lines =<< trim END 927 lines =<< trim END
856 vim9script 928 vim9script
857 class Person 929 class Person
858 this.name: string 930 var name: string
859 this.age: number = 42 931 var age: number = 42
860 this.education: string = "unknown" 932 var education: string = "unknown"
861 933
862 def new(this.name, this.age = v:none, this.education = v:none) 934 def new(this.name, this.age = v:none, this.education = v:none)
863 enddef 935 enddef
864 endclass 936 endclass
865 937
870 # Using a specific value to initialize an instance variable in the new() 942 # Using a specific value to initialize an instance variable in the new()
871 # method. 943 # method.
872 lines =<< trim END 944 lines =<< trim END
873 vim9script 945 vim9script
874 class A 946 class A
875 this.val: string 947 var val: string
876 def new(this.val = 'a') 948 def new(this.val = 'a')
877 enddef 949 enddef
878 endclass 950 endclass
879 END 951 END
880 v9.CheckSourceFailure(lines, "E1328: Constructor default value must be v:none: = 'a'", 4) 952 v9.CheckSourceFailure(lines, "E1328: Constructor default value must be v:none: = 'a'", 4)
883 def Test_class_new_with_object_member() 955 def Test_class_new_with_object_member()
884 var lines =<< trim END 956 var lines =<< trim END
885 vim9script 957 vim9script
886 958
887 class C 959 class C
888 this.str: string 960 var str: string
889 this.num: number 961 var num: number
890 def new(this.str, this.num) 962 def new(this.str, this.num)
891 enddef 963 enddef
892 def newVals(this.str, this.num) 964 def newVals(this.str, this.num)
893 enddef 965 enddef
894 endclass 966 endclass
913 985
914 lines =<< trim END 986 lines =<< trim END
915 vim9script 987 vim9script
916 988
917 class C 989 class C
918 this.str: string 990 var str: string
919 this.num: number 991 var num: number
920 def new(this.str, this.num) 992 def new(this.str, this.num)
921 enddef 993 enddef
922 endclass 994 endclass
923 995
924 def Check() 996 def Check()
935 1007
936 lines =<< trim END 1008 lines =<< trim END
937 vim9script 1009 vim9script
938 1010
939 class C 1011 class C
940 this.str: string 1012 var str: string
941 this.num: number 1013 var num: number
942 def newVals(this.str, this.num) 1014 def newVals(this.str, this.num)
943 enddef 1015 enddef
944 endclass 1016 endclass
945 1017
946 def Check() 1018 def Check()
957 1029
958 lines =<< trim END 1030 lines =<< trim END
959 vim9script 1031 vim9script
960 1032
961 class C 1033 class C
962 this.str: string 1034 var str: string
963 def new(str: any) 1035 def new(str: any)
964 enddef 1036 enddef
965 endclass 1037 endclass
966 1038
967 def Check() 1039 def Check()
978 1050
979 # Try using "this." argument in a class method 1051 # Try using "this." argument in a class method
980 lines =<< trim END 1052 lines =<< trim END
981 vim9script 1053 vim9script
982 class A 1054 class A
983 this.val = 10 1055 var val = 10
984 static def Foo(this.val: number) 1056 static def Foo(this.val: number)
985 enddef 1057 enddef
986 endclass 1058 endclass
987 END 1059 END
988 v9.CheckSourceFailure(lines, 'E1390: Cannot use an object variable "this.val" except with the "new" method', 4) 1060 v9.CheckSourceFailure(lines, 'E1390: Cannot use an object variable "this.val" except with the "new" method', 4)
989 1061
990 # Try using "this." argument in an object method 1062 # Try using "this." argument in an object method
991 lines =<< trim END 1063 lines =<< trim END
992 vim9script 1064 vim9script
993 class A 1065 class A
994 this.val = 10 1066 var val = 10
995 def Foo(this.val: number) 1067 def Foo(this.val: number)
996 enddef 1068 enddef
997 endclass 1069 endclass
998 END 1070 END
999 v9.CheckSourceFailure(lines, 'E1390: Cannot use an object variable "this.val" except with the "new" method', 4) 1071 v9.CheckSourceFailure(lines, 'E1390: Cannot use an object variable "this.val" except with the "new" method', 4)
1001 1073
1002 def Test_class_object_member_inits() 1074 def Test_class_object_member_inits()
1003 var lines =<< trim END 1075 var lines =<< trim END
1004 vim9script 1076 vim9script
1005 class TextPosition 1077 class TextPosition
1006 this.lnum: number 1078 var lnum: number
1007 this.col = 1 1079 var col = 1
1008 this.addcol: number = 2 1080 var addcol: number = 2
1009 endclass 1081 endclass
1010 1082
1011 var pos = TextPosition.new() 1083 var pos = TextPosition.new()
1012 assert_equal(0, pos.lnum) 1084 assert_equal(0, pos.lnum)
1013 assert_equal(1, pos.col) 1085 assert_equal(1, pos.col)
1016 v9.CheckSourceSuccess(lines) 1088 v9.CheckSourceSuccess(lines)
1017 1089
1018 lines =<< trim END 1090 lines =<< trim END
1019 vim9script 1091 vim9script
1020 class TextPosition 1092 class TextPosition
1021 this.lnum 1093 var lnum
1022 this.col = 1 1094 var col = 1
1023 endclass 1095 endclass
1024 END 1096 END
1025 v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3) 1097 v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3)
1026 1098
1027 # If the type is not specified for a member, then it should be set during 1099 # If the type is not specified for a member, then it should be set during
1034 init_count += 1 1106 init_count += 1
1035 return 'foo' 1107 return 'foo'
1036 enddef 1108 enddef
1037 1109
1038 class A 1110 class A
1039 this.str1 = Init() 1111 var str1 = Init()
1040 this.str2: string = Init() 1112 var str2: string = Init()
1041 this.col = 1 1113 var col = 1
1042 endclass 1114 endclass
1043 1115
1044 assert_equal(init_count, 0) 1116 assert_equal(init_count, 0)
1045 var a = A.new() 1117 var a = A.new()
1046 assert_equal(init_count, 2) 1118 assert_equal(init_count, 2)
1049 1121
1050 # Test for initializing an object member with an unknown variable/type 1122 # Test for initializing an object member with an unknown variable/type
1051 lines =<< trim END 1123 lines =<< trim END
1052 vim9script 1124 vim9script
1053 class A 1125 class A
1054 this.value = init_val 1126 var value = init_val
1055 endclass 1127 endclass
1056 var a = A.new() 1128 var a = A.new()
1057 END 1129 END
1058 v9.CheckSourceFailure(lines, 'E1001: Variable not found: init_val', 1) 1130 v9.CheckSourceFailure(lines, 'E1001: Variable not found: init_val', 1)
1059 1131
1060 # Test for initializing an object member with an special type 1132 # Test for initializing an object member with an special type
1061 lines =<< trim END 1133 lines =<< trim END
1062 vim9script 1134 vim9script
1063 class A 1135 class A
1064 this.value: void 1136 var value: void
1065 endclass 1137 endclass
1066 END 1138 END
1067 v9.CheckSourceFailure(lines, 'E1330: Invalid type for object variable: void', 3) 1139 v9.CheckSourceFailure(lines, 'E1330: Invalid type for object variable: void', 3)
1068 enddef 1140 enddef
1069 1141
1070 " Test for instance variable access 1142 " Test for instance variable access
1071 def Test_instance_variable_access() 1143 def Test_instance_variable_access()
1072 var lines =<< trim END 1144 var lines =<< trim END
1073 vim9script 1145 vim9script
1074 class Triple 1146 class Triple
1075 this._one = 1 1147 var _one = 1
1076 this.two = 2 1148 var two = 2
1077 public this.three = 3 1149 public var three = 3
1078 1150
1079 def GetOne(): number 1151 def GetOne(): number
1080 return this._one 1152 return this._one
1081 enddef 1153 enddef
1082 endclass 1154 endclass
1098 1170
1099 # Test for a public member variable name beginning with an underscore 1171 # Test for a public member variable name beginning with an underscore
1100 lines =<< trim END 1172 lines =<< trim END
1101 vim9script 1173 vim9script
1102 class A 1174 class A
1103 public this._val = 10 1175 public var _val = 10
1104 endclass 1176 endclass
1105 END 1177 END
1106 v9.CheckSourceFailure(lines, 'E1332: Public variable name cannot start with underscore: public this._val = 10', 3) 1178 v9.CheckSourceFailure(lines, 'E1332: Public variable name cannot start with underscore: public var _val = 10', 3)
1107 1179
1108 lines =<< trim END 1180 lines =<< trim END
1109 vim9script 1181 vim9script
1110 1182
1111 class MyCar 1183 class MyCar
1112 this.make: string 1184 var make: string
1113 this.age = 5 1185 var age = 5
1114 1186
1115 def new(make_arg: string) 1187 def new(make_arg: string)
1116 this.make = make_arg 1188 this.make = make_arg
1117 enddef 1189 enddef
1118 1190
1143 1215
1144 lines =<< trim END 1216 lines =<< trim END
1145 vim9script 1217 vim9script
1146 1218
1147 class MyCar 1219 class MyCar
1148 this.make: string 1220 var make: string
1149 1221
1150 def new(make_arg: string) 1222 def new(make_arg: string)
1151 this.make = make_arg 1223 this.make = make_arg
1152 enddef 1224 enddef
1153 endclass 1225 endclass
1159 1231
1160 lines =<< trim END 1232 lines =<< trim END
1161 vim9script 1233 vim9script
1162 1234
1163 class Foo 1235 class Foo
1164 this.x: list<number> = [] 1236 var x: list<number> = []
1165 1237
1166 def Add(n: number): any 1238 def Add(n: number): any
1167 this.x->add(n) 1239 this.x->add(n)
1168 return this 1240 return this
1169 enddef 1241 enddef
1185 1257
1186 # Test for "public" cannot be abbreviated 1258 # Test for "public" cannot be abbreviated
1187 lines =<< trim END 1259 lines =<< trim END
1188 vim9script 1260 vim9script
1189 class Something 1261 class Something
1190 pub this.val = 1 1262 pub var val = 1
1191 endclass 1263 endclass
1192 END 1264 END
1193 v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: pub this.val = 1', 3) 1265 v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: pub var val = 1', 3)
1194 1266
1195 # Test for "public" keyword must be followed by "this" or "static". 1267 # Test for "public" keyword must be followed by "var" or "static".
1196 lines =<< trim END 1268 lines =<< trim END
1197 vim9script 1269 vim9script
1198 class Something 1270 class Something
1199 public val = 1 1271 public val = 1
1200 endclass 1272 endclass
1201 END 1273 END
1202 v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "this" or "static"', 3) 1274 v9.CheckSourceFailure(lines, 'E1331: Public must be followed by "var" or "static"', 3)
1203 1275
1204 # Modify a instance variable using the class name in the script context 1276 # Modify a instance variable using the class name in the script context
1205 lines =<< trim END 1277 lines =<< trim END
1206 vim9script 1278 vim9script
1207 class A 1279 class A
1208 public this.val = 1 1280 public var val = 1
1209 endclass 1281 endclass
1210 A.val = 1 1282 A.val = 1
1211 END 1283 END
1212 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object', 5) 1284 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object', 5)
1213 1285
1214 # Read a instance variable using the class name in the script context 1286 # Read a instance variable using the class name in the script context
1215 lines =<< trim END 1287 lines =<< trim END
1216 vim9script 1288 vim9script
1217 class A 1289 class A
1218 public this.val = 1 1290 public var val = 1
1219 endclass 1291 endclass
1220 var i = A.val 1292 var i = A.val
1221 END 1293 END
1222 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object', 5) 1294 v9.CheckSourceFailure(lines, 'E1376: Object variable "val" accessible only using class "A" object', 5)
1223 1295
1224 # Modify a instance variable using the class name in a def function 1296 # Modify a instance variable using the class name in a def function
1225 lines =<< trim END 1297 lines =<< trim END
1226 vim9script 1298 vim9script
1227 class A 1299 class A
1228 public this.val = 1 1300 public var val = 1
1229 endclass 1301 endclass
1230 def T() 1302 def T()
1231 A.val = 1 1303 A.val = 1
1232 enddef 1304 enddef
1233 T() 1305 T()
1236 1308
1237 # Read a instance variable using the class name in a def function 1309 # Read a instance variable using the class name in a def function
1238 lines =<< trim END 1310 lines =<< trim END
1239 vim9script 1311 vim9script
1240 class A 1312 class A
1241 public this.val = 1 1313 public var val = 1
1242 endclass 1314 endclass
1243 def T() 1315 def T()
1244 var i = A.val 1316 var i = A.val
1245 enddef 1317 enddef
1246 T() 1318 T()
1249 1321
1250 # Access from child class extending a class: 1322 # Access from child class extending a class:
1251 lines =<< trim END 1323 lines =<< trim END
1252 vim9script 1324 vim9script
1253 class A 1325 class A
1254 this.ro_obj_var = 10 1326 var ro_obj_var = 10
1255 public this.rw_obj_var = 20 1327 public var rw_obj_var = 20
1256 this._priv_obj_var = 30 1328 var _priv_obj_var = 30
1257 endclass 1329 endclass
1258 1330
1259 class B extends A 1331 class B extends A
1260 def Foo() 1332 def Foo()
1261 var x: number 1333 var x: number
1278 def Test_class_variable_access() 1350 def Test_class_variable_access()
1279 # Test for "static" cannot be abbreviated 1351 # Test for "static" cannot be abbreviated
1280 var lines =<< trim END 1352 var lines =<< trim END
1281 vim9script 1353 vim9script
1282 class Something 1354 class Something
1283 stat this.val = 1 1355 stat var val = 1
1284 endclass 1356 endclass
1285 END 1357 END
1286 v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: stat this.val = 1', 3) 1358 v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: stat var val = 1', 3)
1287 1359
1288 # Test for "static" cannot be followed by "this". 1360 # Test for "static" cannot be followed by "public".
1289 lines =<< trim END 1361 lines =<< trim END
1290 vim9script 1362 vim9script
1291 class Something 1363 class Something
1292 static this.val = 1 1364 static public var val = 1
1293 endclass 1365 endclass
1294 END 1366 END
1295 v9.CheckSourceFailure(lines, 'E1368: Static cannot be followed by "this" in a variable name', 3) 1367 v9.CheckSourceFailure(lines, 'E1368: Static must be followed by "var" or "def"', 3)
1296
1297 # Test for "static" cannot be followed by "public".
1298 lines =<< trim END
1299 vim9script
1300 class Something
1301 static public val = 1
1302 endclass
1303 END
1304 v9.CheckSourceFailure(lines, 'E1022: Type or initialization required', 3)
1305 1368
1306 # A readonly class variable cannot be modified from a child class 1369 # A readonly class variable cannot be modified from a child class
1307 lines =<< trim END 1370 lines =<< trim END
1308 vim9script 1371 vim9script
1309 class A 1372 class A
1310 static ro_class_var = 40 1373 static var ro_class_var = 40
1311 endclass 1374 endclass
1312 1375
1313 class B extends A 1376 class B extends A
1314 def Foo() 1377 def Foo()
1315 A.ro_class_var = 50 1378 A.ro_class_var = 50
1323 1386
1324 # A protected class variable cannot be accessed from a child class 1387 # A protected class variable cannot be accessed from a child class
1325 lines =<< trim END 1388 lines =<< trim END
1326 vim9script 1389 vim9script
1327 class A 1390 class A
1328 static _priv_class_var = 60 1391 static var _priv_class_var = 60
1329 endclass 1392 endclass
1330 1393
1331 class B extends A 1394 class B extends A
1332 def Foo() 1395 def Foo()
1333 var i = A._priv_class_var 1396 var i = A._priv_class_var
1341 1404
1342 # A protected class variable cannot be modified from a child class 1405 # A protected class variable cannot be modified from a child class
1343 lines =<< trim END 1406 lines =<< trim END
1344 vim9script 1407 vim9script
1345 class A 1408 class A
1346 static _priv_class_var = 60 1409 static var _priv_class_var = 60
1347 endclass 1410 endclass
1348 1411
1349 class B extends A 1412 class B extends A
1350 def Foo() 1413 def Foo()
1351 A._priv_class_var = 0 1414 A._priv_class_var = 0
1359 1422
1360 # Access from child class extending a class and from script context 1423 # Access from child class extending a class and from script context
1361 lines =<< trim END 1424 lines =<< trim END
1362 vim9script 1425 vim9script
1363 class A 1426 class A
1364 static ro_class_var = 10 1427 static var ro_class_var = 10
1365 public static rw_class_var = 20 1428 public static var rw_class_var = 20
1366 static _priv_class_var = 30 1429 static var _priv_class_var = 30
1367 endclass 1430 endclass
1368 1431
1369 class B extends A 1432 class B extends A
1370 def Foo() 1433 def Foo()
1371 var x: number 1434 var x: number
1390 1453
1391 def Test_class_object_compare() 1454 def Test_class_object_compare()
1392 var class_lines =<< trim END 1455 var class_lines =<< trim END
1393 vim9script 1456 vim9script
1394 class Item 1457 class Item
1395 this.nr = 0 1458 var nr = 0
1396 this.name = 'xx' 1459 var name = 'xx'
1397 endclass 1460 endclass
1398 END 1461 END
1399 1462
1400 # used at the script level and in a compiled function 1463 # used at the script level and in a compiled function
1401 var test_lines =<< trim END 1464 var test_lines =<< trim END
1433 def Test_object_type() 1496 def Test_object_type()
1434 var lines =<< trim END 1497 var lines =<< trim END
1435 vim9script 1498 vim9script
1436 1499
1437 class One 1500 class One
1438 this.one = 1 1501 var one = 1
1439 endclass 1502 endclass
1440 class Two 1503 class Two
1441 this.two = 2 1504 var two = 2
1442 endclass 1505 endclass
1443 class TwoMore extends Two 1506 class TwoMore extends Two
1444 this.more = 9 1507 var more = 9
1445 endclass 1508 endclass
1446 1509
1447 var o: One = One.new() 1510 var o: One = One.new()
1448 var t: Two = Two.new() 1511 var t: Two = Two.new()
1449 var m: TwoMore = TwoMore.new() 1512 var m: TwoMore = TwoMore.new()
1455 1518
1456 lines =<< trim END 1519 lines =<< trim END
1457 vim9script 1520 vim9script
1458 1521
1459 class One 1522 class One
1460 this.one = 1 1523 var one = 1
1461 endclass 1524 endclass
1462 class Two 1525 class Two
1463 this.two = 2 1526 var two = 2
1464 endclass 1527 endclass
1465 1528
1466 var o: One = Two.new() 1529 var o: One = Two.new()
1467 END 1530 END
1468 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>', 10) 1531 v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<One> but got object<Two>', 10)
1472 1535
1473 interface One 1536 interface One
1474 def GetMember(): number 1537 def GetMember(): number
1475 endinterface 1538 endinterface
1476 class Two implements One 1539 class Two implements One
1477 this.one = 1 1540 var one = 1
1478 def GetMember(): number 1541 def GetMember(): number
1479 return this.one 1542 return this.one
1480 enddef 1543 enddef
1481 endclass 1544 endclass
1482 1545
1487 1550
1488 lines =<< trim END 1551 lines =<< trim END
1489 vim9script 1552 vim9script
1490 1553
1491 class Num 1554 class Num
1492 this.n: number = 0 1555 var n: number = 0
1493 endclass 1556 endclass
1494 1557
1495 def Ref(name: string): func(Num): Num 1558 def Ref(name: string): func(Num): Num
1496 return (arg: Num): Num => { 1559 return (arg: Num): Num => {
1497 return eval(name)(arg) 1560 return eval(name)(arg)
1509 def Test_class_member() 1572 def Test_class_member()
1510 # check access rules 1573 # check access rules
1511 var lines =<< trim END 1574 var lines =<< trim END
1512 vim9script 1575 vim9script
1513 class TextPos 1576 class TextPos
1514 this.lnum = 1 1577 var lnum = 1
1515 this.col = 1 1578 var col = 1
1516 static counter = 0 1579 static var counter = 0
1517 static _secret = 7 1580 static var _secret = 7
1518 public static anybody = 42 1581 public static var anybody = 42
1519 1582
1520 static def AddToCounter(nr: number) 1583 static def AddToCounter(nr: number)
1521 counter += nr 1584 counter += nr
1522 enddef 1585 enddef
1523 endclass 1586 endclass
1549 1612
1550 # example in the help 1613 # example in the help
1551 lines =<< trim END 1614 lines =<< trim END
1552 vim9script 1615 vim9script
1553 class OtherThing 1616 class OtherThing
1554 this.size: number 1617 var size: number
1555 static totalSize: number 1618 static var totalSize: number
1556 1619
1557 def new(this.size) 1620 def new(this.size)
1558 totalSize += this.size 1621 totalSize += this.size
1559 enddef 1622 enddef
1560 endclass 1623 endclass
1569 # using static class member twice 1632 # using static class member twice
1570 lines =<< trim END 1633 lines =<< trim END
1571 vim9script 1634 vim9script
1572 1635
1573 class HTML 1636 class HTML
1574 static author: string = 'John Doe' 1637 static var author: string = 'John Doe'
1575 1638
1576 static def MacroSubstitute(s: string): string 1639 static def MacroSubstitute(s: string): string
1577 return substitute(s, '{{author}}', author, 'gi') 1640 return substitute(s, '{{author}}', author, 'gi')
1578 enddef 1641 enddef
1579 endclass 1642 endclass
1586 # access protected member in lambda 1649 # access protected member in lambda
1587 lines =<< trim END 1650 lines =<< trim END
1588 vim9script 1651 vim9script
1589 1652
1590 class Foo 1653 class Foo
1591 this._x: number = 0 1654 var _x: number = 0
1592 1655
1593 def Add(n: number): number 1656 def Add(n: number): number
1594 const F = (): number => this._x + n 1657 const F = (): number => this._x + n
1595 return F() 1658 return F()
1596 enddef 1659 enddef
1604 # access protected member in lambda body 1667 # access protected member in lambda body
1605 lines =<< trim END 1668 lines =<< trim END
1606 vim9script 1669 vim9script
1607 1670
1608 class Foo 1671 class Foo
1609 this._x: number = 6 1672 var _x: number = 6
1610 1673
1611 def Add(n: number): number 1674 def Add(n: number): number
1612 var Lam = () => { 1675 var Lam = () => {
1613 this._x = this._x + n 1676 this._x = this._x + n
1614 } 1677 }
1625 # check shadowing 1688 # check shadowing
1626 lines =<< trim END 1689 lines =<< trim END
1627 vim9script 1690 vim9script
1628 1691
1629 class Some 1692 class Some
1630 static count = 0 1693 static var count = 0
1631 def Method(count: number) 1694 def Method(count: number)
1632 echo count 1695 echo count
1633 enddef 1696 enddef
1634 endclass 1697 endclass
1635 1698
1641 # Use a local variable in a method with the same name as a class variable 1704 # Use a local variable in a method with the same name as a class variable
1642 lines =<< trim END 1705 lines =<< trim END
1643 vim9script 1706 vim9script
1644 1707
1645 class Some 1708 class Some
1646 static count = 0 1709 static var count = 0
1647 def Method(arg: number) 1710 def Method(arg: number)
1648 var count = 3 1711 var count = 3
1649 echo arg count 1712 echo arg count
1650 enddef 1713 enddef
1651 endclass 1714 endclass
1657 1720
1658 # Test for using an invalid type for a member variable 1721 # Test for using an invalid type for a member variable
1659 lines =<< trim END 1722 lines =<< trim END
1660 vim9script 1723 vim9script
1661 class A 1724 class A
1662 this.val: xxx 1725 var val: xxx
1663 endclass 1726 endclass
1664 END 1727 END
1665 v9.CheckSourceFailure(lines, 'E1010: Type not recognized: xxx', 3) 1728 v9.CheckSourceFailure(lines, 'E1010: Type not recognized: xxx', 3)
1666 1729
1667 # Test for setting a member on a null object 1730 # Test for setting a member on a null object
1668 lines =<< trim END 1731 lines =<< trim END
1669 vim9script 1732 vim9script
1670 class A 1733 class A
1671 public this.val: string 1734 public var val: string
1672 endclass 1735 endclass
1673 1736
1674 def F() 1737 def F()
1675 var obj: A 1738 var obj: A
1676 obj.val = "" 1739 obj.val = ""
1681 1744
1682 # Test for accessing a member on a null object 1745 # Test for accessing a member on a null object
1683 lines =<< trim END 1746 lines =<< trim END
1684 vim9script 1747 vim9script
1685 class A 1748 class A
1686 this.val: string 1749 var val: string
1687 endclass 1750 endclass
1688 1751
1689 def F() 1752 def F()
1690 var obj: A 1753 var obj: A
1691 echo obj.val 1754 echo obj.val
1696 1759
1697 # Test for setting a member on a null object, at script level 1760 # Test for setting a member on a null object, at script level
1698 lines =<< trim END 1761 lines =<< trim END
1699 vim9script 1762 vim9script
1700 class A 1763 class A
1701 public this.val: string 1764 public var val: string
1702 endclass 1765 endclass
1703 1766
1704 var obj: A 1767 var obj: A
1705 obj.val = "" 1768 obj.val = ""
1706 END 1769 END
1708 1771
1709 # Test for accessing a member on a null object, at script level 1772 # Test for accessing a member on a null object, at script level
1710 lines =<< trim END 1773 lines =<< trim END
1711 vim9script 1774 vim9script
1712 class A 1775 class A
1713 this.val: string 1776 var val: string
1714 endclass 1777 endclass
1715 1778
1716 var obj: A 1779 var obj: A
1717 echo obj.val 1780 echo obj.val
1718 END 1781 END
1721 # Test for no space before or after the '=' when initializing a member 1784 # Test for no space before or after the '=' when initializing a member
1722 # variable 1785 # variable
1723 lines =<< trim END 1786 lines =<< trim END
1724 vim9script 1787 vim9script
1725 class A 1788 class A
1726 this.val: number= 10 1789 var val: number= 10
1727 endclass 1790 endclass
1728 END 1791 END
1729 v9.CheckSourceFailure(lines, "E1004: White space required before and after '='", 3) 1792 v9.CheckSourceFailure(lines, "E1004: White space required before and after '='", 3)
1730 lines =<< trim END 1793 lines =<< trim END
1731 vim9script 1794 vim9script
1732 class A 1795 class A
1733 this.val: number =10 1796 var val: number =10
1734 endclass 1797 endclass
1735 END 1798 END
1736 v9.CheckSourceFailure(lines, "E1004: White space required before and after '='", 3) 1799 v9.CheckSourceFailure(lines, "E1004: White space required before and after '='", 3)
1737 1800
1738 # Access a non-existing member 1801 # Access a non-existing member
1751 def Test_defining_class_message() 1814 def Test_defining_class_message()
1752 var lines =<< trim END 1815 var lines =<< trim END
1753 vim9script 1816 vim9script
1754 1817
1755 class Base 1818 class Base
1756 this._v1: list<list<number>> 1819 var _v1: list<list<number>>
1757 endclass 1820 endclass
1758 1821
1759 class Child extends Base 1822 class Child extends Base
1760 endclass 1823 endclass
1761 1824
1765 v9.CheckSourceFailure(lines, 'E1333: Cannot access protected variable "_v1" in class "Base"', 11) 1828 v9.CheckSourceFailure(lines, 'E1333: Cannot access protected variable "_v1" in class "Base"', 11)
1766 lines =<< trim END 1829 lines =<< trim END
1767 vim9script 1830 vim9script
1768 1831
1769 class Base 1832 class Base
1770 this._v1: list<list<number>> 1833 var _v1: list<list<number>>
1771 endclass 1834 endclass
1772 1835
1773 class Child extends Base 1836 class Child extends Base
1774 endclass 1837 endclass
1775 1838
1782 v9.CheckSourceFailure(lines, 'E1333: Cannot access protected variable "_v1" in class "Base"', 2) 1845 v9.CheckSourceFailure(lines, 'E1333: Cannot access protected variable "_v1" in class "Base"', 2)
1783 lines =<< trim END 1846 lines =<< trim END
1784 vim9script 1847 vim9script
1785 1848
1786 class Base 1849 class Base
1787 this.v1: list<list<number>> 1850 var v1: list<list<number>>
1788 endclass 1851 endclass
1789 1852
1790 class Child extends Base 1853 class Child extends Base
1791 endclass 1854 endclass
1792 1855
1796 v9.CheckSourceFailure(lines, 'E1335: Variable "v1" in class "Base" is not writable', 11) 1859 v9.CheckSourceFailure(lines, 'E1335: Variable "v1" in class "Base" is not writable', 11)
1797 lines =<< trim END 1860 lines =<< trim END
1798 vim9script 1861 vim9script
1799 1862
1800 class Base 1863 class Base
1801 this.v1: list<list<number>> 1864 var v1: list<list<number>>
1802 endclass 1865 endclass
1803 1866
1804 class Child extends Base 1867 class Child extends Base
1805 endclass 1868 endclass
1806 1869
1819 1882
1820 class Base0 1883 class Base0
1821 endclass 1884 endclass
1822 1885
1823 class Base extends Base0 1886 class Base extends Base0
1824 this._v1: list<list<number>> 1887 var _v1: list<list<number>>
1825 endclass 1888 endclass
1826 1889
1827 class Child extends Base 1890 class Child extends Base
1828 endclass 1891 endclass
1829 1892
1845 1908
1846 class Base extends Base0 1909 class Base extends Base0
1847 endclass 1910 endclass
1848 1911
1849 class Child extends Base 1912 class Child extends Base
1850 this._v1: list<list<number>> 1913 var _v1: list<list<number>>
1851 endclass 1914 endclass
1852 1915
1853 def F() 1916 def F()
1854 var o = Child.new() 1917 var o = Child.new()
1855 var x = o._v1 1918 var x = o._v1
1862 func Test_class_garbagecollect() 1925 func Test_class_garbagecollect()
1863 let lines =<< trim END 1926 let lines =<< trim END
1864 vim9script 1927 vim9script
1865 1928
1866 class Point 1929 class Point
1867 this.p = [2, 3] 1930 var p = [2, 3]
1868 static pl = ['a', 'b'] 1931 static var pl = ['a', 'b']
1869 static pd = {a: 'a', b: 'b'} 1932 static var pd = {a: 'a', b: 'b'}
1870 endclass 1933 endclass
1871 1934
1872 echo Point.pl Point.pd 1935 echo Point.pl Point.pd
1873 call test_garbagecollect_now() 1936 call test_garbagecollect_now()
1874 echo Point.pl Point.pd 1937 echo Point.pl Point.pd
1880 1943
1881 interface View 1944 interface View
1882 endinterface 1945 endinterface
1883 1946
1884 class Widget 1947 class Widget
1885 this.view: View 1948 var view: View
1886 endclass 1949 endclass
1887 1950
1888 class MyView implements View 1951 class MyView implements View
1889 this.widget: Widget 1952 var widget: Widget
1890 1953
1891 def new() 1954 def new()
1892 # this will result in a circular reference to this object 1955 # this will result in a circular reference to this object
1893 this.widget = Widget.new(this) 1956 var widget = Widget.new(this)
1894 enddef 1957 enddef
1895 endclass 1958 endclass
1896 1959
1897 var view = MyView.new() 1960 var view = MyView.new()
1898 1961
1907 func Test_interface_garbagecollect() 1970 func Test_interface_garbagecollect()
1908 let lines =<< trim END 1971 let lines =<< trim END
1909 vim9script 1972 vim9script
1910 1973
1911 interface I 1974 interface I
1912 this.ro_obj_var: number 1975 var ro_obj_var: number
1913 1976
1914 def ObjFoo(): number 1977 def ObjFoo(): number
1915 endinterface 1978 endinterface
1916 1979
1917 class A implements I 1980 class A implements I
1918 static ro_class_var: number = 10 1981 static var ro_class_var: number = 10
1919 public static rw_class_var: number = 20 1982 public static var rw_class_var: number = 20
1920 static _priv_class_var: number = 30 1983 static var _priv_class_var: number = 30
1921 this.ro_obj_var: number = 40 1984 var ro_obj_var: number = 40
1922 this._priv_obj_var: number = 60 1985 var _priv_obj_var: number = 60
1923 1986
1924 static def _ClassBar(): number 1987 static def _ClassBar(): number
1925 return _priv_class_var 1988 return _priv_class_var
1926 enddef 1989 enddef
1927 1990
1950 2013
1951 def Test_class_method() 2014 def Test_class_method()
1952 var lines =<< trim END 2015 var lines =<< trim END
1953 vim9script 2016 vim9script
1954 class Value 2017 class Value
1955 this.value = 0 2018 var value = 0
1956 static objects = 0 2019 static var objects = 0
1957 2020
1958 def new(v: number) 2021 def new(v: number)
1959 this.value = v 2022 this.value = v
1960 ++objects 2023 ++objects
1961 enddef 2024 enddef
1988 # Test for calling a class method from another class method without the class 2051 # Test for calling a class method from another class method without the class
1989 # name prefix. 2052 # name prefix.
1990 lines =<< trim END 2053 lines =<< trim END
1991 vim9script 2054 vim9script
1992 class A 2055 class A
1993 static myList: list<number> = [1] 2056 static var myList: list<number> = [1]
1994 static def Foo(n: number) 2057 static def Foo(n: number)
1995 myList->add(n) 2058 myList->add(n)
1996 enddef 2059 enddef
1997 static def Bar() 2060 static def Bar()
1998 Foo(2) 2061 Foo(2)
2085 2148
2086 def Test_class_object_to_string() 2149 def Test_class_object_to_string()
2087 var lines =<< trim END 2150 var lines =<< trim END
2088 vim9script 2151 vim9script
2089 class TextPosition 2152 class TextPosition
2090 this.lnum = 1 2153 var lnum = 1
2091 this.col = 22 2154 var col = 22
2092 endclass 2155 endclass
2093 2156
2094 assert_equal("class TextPosition", string(TextPosition)) 2157 assert_equal("class TextPosition", string(TextPosition))
2095 2158
2096 var pos = TextPosition.new() 2159 var pos = TextPosition.new()
2101 2164
2102 def Test_interface_basics() 2165 def Test_interface_basics()
2103 var lines =<< trim END 2166 var lines =<< trim END
2104 vim9script 2167 vim9script
2105 interface Something 2168 interface Something
2106 this.ro_var: list<number> 2169 var ro_var: list<number>
2107 def GetCount(): number 2170 def GetCount(): number
2108 endinterface 2171 endinterface
2109 END 2172 END
2110 v9.CheckSourceSuccess(lines) 2173 v9.CheckSourceSuccess(lines)
2111 2174
2112 lines =<< trim END 2175 lines =<< trim END
2113 interface SomethingWrong 2176 interface SomethingWrong
2114 static count = 7 2177 static var count = 7
2115 endinterface 2178 endinterface
2116 END 2179 END
2117 v9.CheckSourceFailure(lines, 'E1342: Interface can only be defined in Vim9 script', 1) 2180 v9.CheckSourceFailure(lines, 'E1342: Interface can only be defined in Vim9 script', 1)
2118 2181
2119 lines =<< trim END 2182 lines =<< trim END
2120 vim9script 2183 vim9script
2121 2184
2122 interface Some 2185 interface Some
2123 this.value: number 2186 var value: number
2124 def Method(value: number) 2187 def Method(value: number)
2125 endinterface 2188 endinterface
2126 END 2189 END
2127 # The argument name and the object member name are the same, but this is not a 2190 # The argument name and the object member name are the same, but this is not a
2128 # problem because object members are always accessed with the "this." prefix. 2191 # problem because object members are always accessed with the "this." prefix.
2129 v9.CheckSourceSuccess(lines) 2192 v9.CheckSourceSuccess(lines)
2130 2193
2131 lines =<< trim END 2194 lines =<< trim END
2132 vim9script 2195 vim9script
2133 interface somethingWrong 2196 interface somethingWrong
2134 static count = 7 2197 static var count = 7
2135 endinterface 2198 endinterface
2136 END 2199 END
2137 v9.CheckSourceFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong', 2) 2200 v9.CheckSourceFailure(lines, 'E1343: Interface name must start with an uppercase letter: somethingWrong', 2)
2138 2201
2139 lines =<< trim END 2202 lines =<< trim END
2140 vim9script 2203 vim9script
2141 interface SomethingWrong 2204 interface SomethingWrong
2142 this.value: string 2205 var value: string
2143 this.count = 7 2206 var count = 7
2144 def GetCount(): number 2207 def GetCount(): number
2145 endinterface 2208 endinterface
2146 END 2209 END
2147 v9.CheckSourceFailure(lines, 'E1344: Cannot initialize a variable in an interface', 4) 2210 v9.CheckSourceFailure(lines, 'E1344: Cannot initialize a variable in an interface', 4)
2148 2211
2149 lines =<< trim END 2212 lines =<< trim END
2150 vim9script 2213 vim9script
2151 interface SomethingWrong 2214 interface SomethingWrong
2152 this.value: string 2215 var value: string
2153 this.count: number 2216 var count: number
2154 def GetCount(): number 2217 def GetCount(): number
2155 return 5 2218 return 5
2156 enddef 2219 enddef
2157 endinterface 2220 endinterface
2158 END 2221 END
2199 def Test_class_implements_interface() 2262 def Test_class_implements_interface()
2200 var lines =<< trim END 2263 var lines =<< trim END
2201 vim9script 2264 vim9script
2202 2265
2203 interface Some 2266 interface Some
2204 this.count: number 2267 var count: number
2205 def Method(nr: number) 2268 def Method(nr: number)
2206 endinterface 2269 endinterface
2207 2270
2208 class SomeImpl implements Some 2271 class SomeImpl implements Some
2209 this.count: number 2272 var count: number
2210 def Method(nr: number) 2273 def Method(nr: number)
2211 echo nr 2274 echo nr
2212 enddef 2275 enddef
2213 endclass 2276 endclass
2214 2277
2215 interface Another 2278 interface Another
2216 this.member: string 2279 var member: string
2217 endinterface 2280 endinterface
2218 2281
2219 class AnotherImpl implements Some, Another 2282 class AnotherImpl implements Some, Another
2220 this.member = 'abc' 2283 var member = 'abc'
2221 this.count = 20 2284 var count = 20
2222 def Method(nr: number) 2285 def Method(nr: number)
2223 echo nr 2286 echo nr
2224 enddef 2287 enddef
2225 endclass 2288 endclass
2226 END 2289 END
2228 2291
2229 lines =<< trim END 2292 lines =<< trim END
2230 vim9script 2293 vim9script
2231 2294
2232 interface Some 2295 interface Some
2233 this.count: number 2296 var count: number
2234 endinterface 2297 endinterface
2235 2298
2236 class SomeImpl implements Some implements Some 2299 class SomeImpl implements Some implements Some
2237 this.count: number 2300 var count: number
2238 endclass 2301 endclass
2239 END 2302 END
2240 v9.CheckSourceFailure(lines, 'E1350: Duplicate "implements"', 7) 2303 v9.CheckSourceFailure(lines, 'E1350: Duplicate "implements"', 7)
2241 2304
2242 lines =<< trim END 2305 lines =<< trim END
2243 vim9script 2306 vim9script
2244 2307
2245 interface Some 2308 interface Some
2246 this.count: number 2309 var count: number
2247 endinterface 2310 endinterface
2248 2311
2249 class SomeImpl implements Some, Some 2312 class SomeImpl implements Some, Some
2250 this.count: number 2313 var count: number
2251 endclass 2314 endclass
2252 END 2315 END
2253 v9.CheckSourceFailure(lines, 'E1351: Duplicate interface after "implements": Some', 7) 2316 v9.CheckSourceFailure(lines, 'E1351: Duplicate interface after "implements": Some', 7)
2254 2317
2255 lines =<< trim END 2318 lines =<< trim END
2256 vim9script 2319 vim9script
2257 2320
2258 interface Some 2321 interface Some
2259 this.counter: number 2322 var counter: number
2260 def Method(nr: number) 2323 def Method(nr: number)
2261 endinterface 2324 endinterface
2262 2325
2263 class SomeImpl implements Some 2326 class SomeImpl implements Some
2264 this.count: number 2327 var count: number
2265 def Method(nr: number) 2328 def Method(nr: number)
2266 echo nr 2329 echo nr
2267 enddef 2330 enddef
2268 endclass 2331 endclass
2269 END 2332 END
2271 2334
2272 lines =<< trim END 2335 lines =<< trim END
2273 vim9script 2336 vim9script
2274 2337
2275 interface Some 2338 interface Some
2276 this.count: number 2339 var count: number
2277 def Methods(nr: number) 2340 def Methods(nr: number)
2278 endinterface 2341 endinterface
2279 2342
2280 class SomeImpl implements Some 2343 class SomeImpl implements Some
2281 this.count: number 2344 var count: number
2282 def Method(nr: number) 2345 def Method(nr: number)
2283 echo nr 2346 echo nr
2284 enddef 2347 enddef
2285 endclass 2348 endclass
2286 END 2349 END
2289 # Check different order of members in class and interface works. 2352 # Check different order of members in class and interface works.
2290 lines =<< trim END 2353 lines =<< trim END
2291 vim9script 2354 vim9script
2292 2355
2293 interface Result 2356 interface Result
2294 this.label: string 2357 var label: string
2295 this.errpos: number 2358 var errpos: number
2296 endinterface 2359 endinterface
2297 2360
2298 # order of members is opposite of interface 2361 # order of members is opposite of interface
2299 class Failure implements Result 2362 class Failure implements Result
2300 public this.lnum: number = 5 2363 public var lnum: number = 5
2301 this.errpos: number = 42 2364 var errpos: number = 42
2302 this.label: string = 'label' 2365 var label: string = 'label'
2303 endclass 2366 endclass
2304 2367
2305 def Test() 2368 def Test()
2306 var result: Result = Failure.new() 2369 var result: Result = Failure.new()
2307 2370
2410 # access superclass interface members from subclass, mix variable order 2473 # access superclass interface members from subclass, mix variable order
2411 lines =<< trim END 2474 lines =<< trim END
2412 vim9script 2475 vim9script
2413 2476
2414 interface I1 2477 interface I1
2415 this.mvar1: number 2478 var mvar1: number
2416 this.mvar2: number 2479 var mvar2: number
2417 endinterface 2480 endinterface
2418 2481
2419 # NOTE: the order is swapped 2482 # NOTE: the order is swapped
2420 class A implements I1 2483 class A implements I1
2421 this.mvar2: number 2484 var mvar2: number
2422 this.mvar1: number 2485 var mvar1: number
2423 public static svar2: number 2486 public static var svar2: number
2424 public static svar1: number 2487 public static var svar1: number
2425 def new() 2488 def new()
2426 svar1 = 11 2489 svar1 = 11
2427 svar2 = 12 2490 svar2 = 12
2428 this.mvar1 = 111 2491 this.mvar1 = 111
2429 this.mvar2 = 112 2492 this.mvar2 = 112
2462 # Two interfaces, one on A, one on B; each has both kinds of variables 2525 # Two interfaces, one on A, one on B; each has both kinds of variables
2463 lines =<< trim END 2526 lines =<< trim END
2464 vim9script 2527 vim9script
2465 2528
2466 interface I1 2529 interface I1
2467 this.mvar1: number 2530 var mvar1: number
2468 this.mvar2: number 2531 var mvar2: number
2469 endinterface 2532 endinterface
2470 2533
2471 interface I2 2534 interface I2
2472 this.mvar3: number 2535 var mvar3: number
2473 this.mvar4: number 2536 var mvar4: number
2474 endinterface 2537 endinterface
2475 2538
2476 class A implements I1 2539 class A implements I1
2477 public static svar1: number 2540 public static var svar1: number
2478 public static svar2: number 2541 public static var svar2: number
2479 this.mvar1: number 2542 var mvar1: number
2480 this.mvar2: number 2543 var mvar2: number
2481 def new() 2544 def new()
2482 svar1 = 11 2545 svar1 = 11
2483 svar2 = 12 2546 svar2 = 12
2484 this.mvar1 = 111 2547 this.mvar1 = 111
2485 this.mvar2 = 112 2548 this.mvar2 = 112
2486 enddef 2549 enddef
2487 endclass 2550 endclass
2488 2551
2489 class B extends A implements I2 2552 class B extends A implements I2
2490 static svar3: number 2553 static var svar3: number
2491 static svar4: number 2554 static var svar4: number
2492 this.mvar3: number 2555 var mvar3: number
2493 this.mvar4: number 2556 var mvar4: number
2494 def new() 2557 def new()
2495 svar3 = 23 2558 svar3 = 23
2496 svar4 = 24 2559 svar4 = 24
2497 this.mvar1 = 121 2560 this.mvar1 = 121
2498 this.mvar2 = 122 2561 this.mvar2 = 122
2500 this.mvar4 = 124 2563 this.mvar4 = 124
2501 enddef 2564 enddef
2502 endclass 2565 endclass
2503 2566
2504 class C extends B 2567 class C extends B
2505 public static svar5: number 2568 public static var svar5: number
2506 def new() 2569 def new()
2507 svar5 = 1001 2570 svar5 = 1001
2508 this.mvar1 = 131 2571 this.mvar1 = 131
2509 this.mvar2 = 132 2572 this.mvar2 = 132
2510 this.mvar3 = 133 2573 this.mvar3 = 133
2684 def Test_class_used_as_type() 2747 def Test_class_used_as_type()
2685 var lines =<< trim END 2748 var lines =<< trim END
2686 vim9script 2749 vim9script
2687 2750
2688 class Point 2751 class Point
2689 this.x = 0 2752 var x = 0
2690 this.y = 0 2753 var y = 0
2691 endclass 2754 endclass
2692 2755
2693 var p: Point 2756 var p: Point
2694 p = Point.new(2, 33) 2757 p = Point.new(2, 33)
2695 assert_equal(2, p.x) 2758 assert_equal(2, p.x)
2699 2762
2700 lines =<< trim END 2763 lines =<< trim END
2701 vim9script 2764 vim9script
2702 2765
2703 interface HasX 2766 interface HasX
2704 this.x: number 2767 var x: number
2705 endinterface 2768 endinterface
2706 2769
2707 class Point implements HasX 2770 class Point implements HasX
2708 this.x = 0 2771 var x = 0
2709 this.y = 0 2772 var y = 0
2710 endclass 2773 endclass
2711 2774
2712 var p: Point 2775 var p: Point
2713 p = Point.new(2, 33) 2776 p = Point.new(2, 33)
2714 var hx = p 2777 var hx = p
2718 2781
2719 lines =<< trim END 2782 lines =<< trim END
2720 vim9script 2783 vim9script
2721 2784
2722 class Point 2785 class Point
2723 this.x = 0 2786 var x = 0
2724 this.y = 0 2787 var y = 0
2725 endclass 2788 endclass
2726 2789
2727 var p: Point 2790 var p: Point
2728 p = 'text' 2791 p = 'text'
2729 END 2792 END
2732 2795
2733 def Test_class_extends() 2796 def Test_class_extends()
2734 var lines =<< trim END 2797 var lines =<< trim END
2735 vim9script 2798 vim9script
2736 class Base 2799 class Base
2737 this.one = 1 2800 var one = 1
2738 def GetOne(): number 2801 def GetOne(): number
2739 return this.one 2802 return this.one
2740 enddef 2803 enddef
2741 endclass 2804 endclass
2742 class Child extends Base 2805 class Child extends Base
2743 this.two = 2 2806 var two = 2
2744 def GetTotal(): number 2807 def GetTotal(): number
2745 return this.one + this.two 2808 return this.one + this.two
2746 enddef 2809 enddef
2747 endclass 2810 endclass
2748 var o = Child.new() 2811 var o = Child.new()
2754 v9.CheckSourceSuccess(lines) 2817 v9.CheckSourceSuccess(lines)
2755 2818
2756 lines =<< trim END 2819 lines =<< trim END
2757 vim9script 2820 vim9script
2758 class Base 2821 class Base
2759 this.one = 1 2822 var one = 1
2760 endclass 2823 endclass
2761 class Child extends Base 2824 class Child extends Base
2762 this.two = 2 2825 var two = 2
2763 endclass 2826 endclass
2764 var o = Child.new(3, 44) 2827 var o = Child.new(3, 44)
2765 assert_equal(3, o.one) 2828 assert_equal(3, o.one)
2766 assert_equal(44, o.two) 2829 assert_equal(44, o.two)
2767 END 2830 END
2768 v9.CheckSourceSuccess(lines) 2831 v9.CheckSourceSuccess(lines)
2769 2832
2770 lines =<< trim END 2833 lines =<< trim END
2771 vim9script 2834 vim9script
2772 class Base 2835 class Base
2773 this.one = 1 2836 var one = 1
2774 endclass 2837 endclass
2775 class Child extends Base extends Base 2838 class Child extends Base extends Base
2776 this.two = 2 2839 var two = 2
2777 endclass 2840 endclass
2778 END 2841 END
2779 v9.CheckSourceFailure(lines, 'E1352: Duplicate "extends"', 5) 2842 v9.CheckSourceFailure(lines, 'E1352: Duplicate "extends"', 5)
2780 2843
2781 lines =<< trim END 2844 lines =<< trim END
2782 vim9script 2845 vim9script
2783 class Child extends BaseClass 2846 class Child extends BaseClass
2784 this.two = 2 2847 var two = 2
2785 endclass 2848 endclass
2786 END 2849 END
2787 v9.CheckSourceFailure(lines, 'E1353: Class name not found: BaseClass', 4) 2850 v9.CheckSourceFailure(lines, 'E1353: Class name not found: BaseClass', 4)
2788 2851
2789 lines =<< trim END 2852 lines =<< trim END
2790 vim9script 2853 vim9script
2791 var SomeVar = 99 2854 var SomeVar = 99
2792 class Child extends SomeVar 2855 class Child extends SomeVar
2793 this.two = 2 2856 var two = 2
2794 endclass 2857 endclass
2795 END 2858 END
2796 v9.CheckSourceFailure(lines, 'E1354: Cannot extend SomeVar', 5) 2859 v9.CheckSourceFailure(lines, 'E1354: Cannot extend SomeVar', 5)
2797 2860
2798 lines =<< trim END 2861 lines =<< trim END
2799 vim9script 2862 vim9script
2800 class Base 2863 class Base
2801 this.name: string 2864 var name: string
2802 def ToString(): string 2865 def ToString(): string
2803 return this.name 2866 return this.name
2804 enddef 2867 enddef
2805 endclass 2868 endclass
2806 2869
2807 class Child extends Base 2870 class Child extends Base
2808 this.age: number 2871 var age: number
2809 def ToString(): string 2872 def ToString(): string
2810 return super.ToString() .. ': ' .. this.age 2873 return super.ToString() .. ': ' .. this.age
2811 enddef 2874 enddef
2812 endclass 2875 endclass
2813 2876
2817 v9.CheckSourceSuccess(lines) 2880 v9.CheckSourceSuccess(lines)
2818 2881
2819 lines =<< trim END 2882 lines =<< trim END
2820 vim9script 2883 vim9script
2821 class Child 2884 class Child
2822 this.age: number 2885 var age: number
2823 def ToString(): number 2886 def ToString(): number
2824 return this.age 2887 return this.age
2825 enddef 2888 enddef
2826 def ToString(): string 2889 def ToString(): string
2827 return this.age 2890 return this.age
2831 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: ToString', 9) 2894 v9.CheckSourceFailure(lines, 'E1355: Duplicate function: ToString', 9)
2832 2895
2833 lines =<< trim END 2896 lines =<< trim END
2834 vim9script 2897 vim9script
2835 class Child 2898 class Child
2836 this.age: number 2899 var age: number
2837 def ToString(): string 2900 def ToString(): string
2838 return super .ToString() .. ': ' .. this.age 2901 return super .ToString() .. ': ' .. this.age
2839 enddef 2902 enddef
2840 endclass 2903 endclass
2841 var o = Child.new(42) 2904 var o = Child.new(42)
2844 v9.CheckSourceFailure(lines, 'E1356: "super" must be followed by a dot', 1) 2907 v9.CheckSourceFailure(lines, 'E1356: "super" must be followed by a dot', 1)
2845 2908
2846 lines =<< trim END 2909 lines =<< trim END
2847 vim9script 2910 vim9script
2848 class Base 2911 class Base
2849 this.name: string 2912 var name: string
2850 def ToString(): string 2913 def ToString(): string
2851 return this.name 2914 return this.name
2852 enddef 2915 enddef
2853 endclass 2916 endclass
2854 2917
2861 v9.CheckSourceFailure(lines, 'E1357: Using "super" not in a class method', 1) 2924 v9.CheckSourceFailure(lines, 'E1357: Using "super" not in a class method', 1)
2862 2925
2863 lines =<< trim END 2926 lines =<< trim END
2864 vim9script 2927 vim9script
2865 class Child 2928 class Child
2866 this.age: number 2929 var age: number
2867 def ToString(): string 2930 def ToString(): string
2868 return super.ToString() .. ': ' .. this.age 2931 return super.ToString() .. ': ' .. this.age
2869 enddef 2932 enddef
2870 endclass 2933 endclass
2871 var o = Child.new(42) 2934 var o = Child.new(42)
2874 v9.CheckSourceFailure(lines, 'E1358: Using "super" not in a child class', 1) 2937 v9.CheckSourceFailure(lines, 'E1358: Using "super" not in a child class', 1)
2875 2938
2876 lines =<< trim END 2939 lines =<< trim END
2877 vim9script 2940 vim9script
2878 class Base 2941 class Base
2879 this.name: string 2942 var name: string
2880 static def ToString(): string 2943 static def ToString(): string
2881 return 'Base class' 2944 return 'Base class'
2882 enddef 2945 enddef
2883 endclass 2946 endclass
2884 2947
2885 class Child extends Base 2948 class Child extends Base
2886 this.age: number 2949 var age: number
2887 def ToString(): string 2950 def ToString(): string
2888 return Base.ToString() .. ': ' .. this.age 2951 return Base.ToString() .. ': ' .. this.age
2889 enddef 2952 enddef
2890 endclass 2953 endclass
2891 2954
2895 v9.CheckSourceSuccess(lines) 2958 v9.CheckSourceSuccess(lines)
2896 2959
2897 lines =<< trim END 2960 lines =<< trim END
2898 vim9script 2961 vim9script
2899 class Base 2962 class Base
2900 this.value = 1 2963 var value = 1
2901 def new(init: number) 2964 def new(init: number)
2902 this.value = number + 1 2965 this.value = number + 1
2903 enddef 2966 enddef
2904 endclass 2967 endclass
2905 class Child extends Base 2968 class Child extends Base
2914 # base class with more than one object member 2977 # base class with more than one object member
2915 lines =<< trim END 2978 lines =<< trim END
2916 vim9script 2979 vim9script
2917 2980
2918 class Result 2981 class Result
2919 this.success: bool 2982 var success: bool
2920 this.value: any = null 2983 var value: any = null
2921 endclass 2984 endclass
2922 2985
2923 class Success extends Result 2986 class Success extends Result
2924 def new(this.value = v:none) 2987 def new(this.value = v:none)
2925 this.success = true 2988 this.success = true
2984 # Using super, Child invokes Base method which has optional arg. #12471 3047 # Using super, Child invokes Base method which has optional arg. #12471
2985 lines =<< trim END 3048 lines =<< trim END
2986 vim9script 3049 vim9script
2987 3050
2988 class Base 3051 class Base
2989 this.success: bool = false 3052 var success: bool = false
2990 def Method(arg = 0) 3053 def Method(arg = 0)
2991 this.success = true 3054 this.success = true
2992 enddef 3055 enddef
2993 endclass 3056 endclass
2994 3057
3006 3069
3007 def Test_class_import() 3070 def Test_class_import()
3008 var lines =<< trim END 3071 var lines =<< trim END
3009 vim9script 3072 vim9script
3010 export class Animal 3073 export class Animal
3011 this.kind: string 3074 var kind: string
3012 this.name: string 3075 var name: string
3013 endclass 3076 endclass
3014 END 3077 END
3015 writefile(lines, 'Xanimal.vim', 'D') 3078 writefile(lines, 'Xanimal.vim', 'D')
3016 3079
3017 lines =<< trim END 3080 lines =<< trim END
3088 3151
3089 def Test_abstract_class() 3152 def Test_abstract_class()
3090 var lines =<< trim END 3153 var lines =<< trim END
3091 vim9script 3154 vim9script
3092 abstract class Base 3155 abstract class Base
3093 this.name: string 3156 var name: string
3094 endclass 3157 endclass
3095 class Person extends Base 3158 class Person extends Base
3096 this.age: number 3159 var age: number
3097 endclass 3160 endclass
3098 var p: Base = Person.new('Peter', 42) 3161 var p: Base = Person.new('Peter', 42)
3099 assert_equal('Peter', p.name) 3162 assert_equal('Peter', p.name)
3100 assert_equal(42, p.age) 3163 assert_equal(42, p.age)
3101 END 3164 END
3102 v9.CheckSourceSuccess(lines) 3165 v9.CheckSourceSuccess(lines)
3103 3166
3104 lines =<< trim END 3167 lines =<< trim END
3105 vim9script 3168 vim9script
3106 abstract class Base 3169 abstract class Base
3107 this.name: string 3170 var name: string
3108 endclass 3171 endclass
3109 class Person extends Base 3172 class Person extends Base
3110 this.age: number 3173 var age: number
3111 endclass 3174 endclass
3112 var p = Base.new('Peter') 3175 var p = Base.new('Peter')
3113 END 3176 END
3114 v9.CheckSourceFailure(lines, 'E1325: Method "new" not found in class "Base"', 8) 3177 v9.CheckSourceFailure(lines, 'E1325: Method "new" not found in class "Base"', 8)
3115 3178
3116 lines =<< trim END 3179 lines =<< trim END
3117 abstract class Base 3180 abstract class Base
3118 this.name: string 3181 var name: string
3119 endclass 3182 endclass
3120 END 3183 END
3121 v9.CheckSourceFailure(lines, 'E1316: Class can only be defined in Vim9 script', 1) 3184 v9.CheckSourceFailure(lines, 'E1316: Class can only be defined in Vim9 script', 1)
3122 3185
3123 # Abstract class cannot have a "new" function 3186 # Abstract class cannot have a "new" function
3134 def Test_closure_in_class() 3197 def Test_closure_in_class()
3135 var lines =<< trim END 3198 var lines =<< trim END
3136 vim9script 3199 vim9script
3137 3200
3138 class Foo 3201 class Foo
3139 this.y: list<string> = ['B'] 3202 var y: list<string> = ['B']
3140 3203
3141 def new() 3204 def new()
3142 g:result = filter(['A', 'B'], (_, v) => index(this.y, v) == -1) 3205 g:result = filter(['A', 'B'], (_, v) => index(this.y, v) == -1)
3143 enddef 3206 enddef
3144 endclass 3207 endclass
3199 vim9script 3262 vim9script
3200 3263
3201 var createdObject: any 3264 var createdObject: any
3202 3265
3203 class A 3266 class A
3204 this.val1: number 3267 var val1: number
3205 this.val2: number 3268 var val2: number
3206 static def CreateA(...args: list<any>): any 3269 static def CreateA(...args: list<any>): any
3207 createdObject = call(A.new, args) 3270 createdObject = call(A.new, args)
3208 return createdObject 3271 return createdObject
3209 enddef 3272 enddef
3210 endclass 3273 endclass
3298 3361
3299 class Observer 3362 class Observer
3300 endclass 3363 endclass
3301 3364
3302 class Property 3365 class Property
3303 this.value: any 3366 var value: any
3304 3367
3305 def Set(v: any) 3368 def Set(v: any)
3306 if v != this.value 3369 if v != this.value
3307 this.value = v 3370 this.value = v
3308 endif 3371 endif
3311 def Register(observer: Observer) 3374 def Register(observer: Observer)
3312 enddef 3375 enddef
3313 endclass 3376 endclass
3314 3377
3315 class Bool extends Property 3378 class Bool extends Property
3316 this.value2: bool 3379 var value2: bool
3317 endclass 3380 endclass
3318 3381
3319 def Observe(obj: Property, who: Observer) 3382 def Observe(obj: Property, who: Observer)
3320 obj.Register(who) 3383 obj.Register(who)
3321 enddef 3384 enddef
3489 def Test_call_method_in_parent_class() 3552 def Test_call_method_in_parent_class()
3490 var lines =<< trim END 3553 var lines =<< trim END
3491 vim9script 3554 vim9script
3492 3555
3493 class Widget 3556 class Widget
3494 this._lnum: number = 1 3557 var _lnum: number = 1
3495 3558
3496 def SetY(lnum: number) 3559 def SetY(lnum: number)
3497 this._lnum = lnum 3560 this._lnum = lnum
3498 enddef 3561 enddef
3499 3562
3599 def Test_multi_level_member_access() 3662 def Test_multi_level_member_access()
3600 var lines =<< trim END 3663 var lines =<< trim END
3601 vim9script 3664 vim9script
3602 3665
3603 class A 3666 class A
3604 public this.val1: number = 0 3667 public var val1: number = 0
3605 endclass 3668 endclass
3606 3669
3607 class B extends A 3670 class B extends A
3608 public this.val2: number = 0 3671 public var val2: number = 0
3609 endclass 3672 endclass
3610 3673
3611 class C extends B 3674 class C extends B
3612 public this.val3: number = 0 3675 public var val3: number = 0
3613 endclass 3676 endclass
3614 3677
3615 def A_members(a: A) 3678 def A_members(a: A)
3616 a.val1 += 1 3679 a.val1 += 1
3617 enddef 3680 enddef
3667 # new() uses the default return type and there is no return statement 3730 # new() uses the default return type and there is no return statement
3668 var lines =<< trim END 3731 var lines =<< trim END
3669 vim9script 3732 vim9script
3670 3733
3671 class C 3734 class C
3672 this._bufnr: number 3735 var _bufnr: number
3673 3736
3674 def new(this._bufnr) 3737 def new(this._bufnr)
3675 if !bufexists(this._bufnr) 3738 if !bufexists(this._bufnr)
3676 this._bufnr = -1 3739 this._bufnr = -1
3677 endif 3740 endif
3697 # new() uses the default return type and an empty 'return' statement 3760 # new() uses the default return type and an empty 'return' statement
3698 lines =<< trim END 3761 lines =<< trim END
3699 vim9script 3762 vim9script
3700 3763
3701 class C 3764 class C
3702 this._bufnr: number 3765 var _bufnr: number
3703 3766
3704 def new(this._bufnr) 3767 def new(this._bufnr)
3705 if !bufexists(this._bufnr) 3768 if !bufexists(this._bufnr)
3706 this._bufnr = -1 3769 this._bufnr = -1
3707 return 3770 return
3728 # new() uses "any" return type and returns "this" 3791 # new() uses "any" return type and returns "this"
3729 lines =<< trim END 3792 lines =<< trim END
3730 vim9script 3793 vim9script
3731 3794
3732 class C 3795 class C
3733 this._bufnr: number 3796 var _bufnr: number
3734 3797
3735 def new(this._bufnr): any 3798 def new(this._bufnr): any
3736 if !bufexists(this._bufnr) 3799 if !bufexists(this._bufnr)
3737 this._bufnr = -1 3800 this._bufnr = -1
3738 return this 3801 return this
3745 # new() uses 'Dict' return type and returns a Dict 3808 # new() uses 'Dict' return type and returns a Dict
3746 lines =<< trim END 3809 lines =<< trim END
3747 vim9script 3810 vim9script
3748 3811
3749 class C 3812 class C
3750 this._state: dict<any> 3813 var _state: dict<any>
3751 3814
3752 def new(): dict<any> 3815 def new(): dict<any>
3753 this._state = {} 3816 this._state = {}
3754 return this._state 3817 return this._state
3755 enddef 3818 enddef
3776 return "hello" 3839 return "hello"
3777 endif 3840 endif
3778 enddef 3841 enddef
3779 3842
3780 class C 3843 class C
3781 this._foo: bool = F() 3844 var _foo: bool = F()
3782 endclass 3845 endclass
3783 3846
3784 var c1 = C.new() 3847 var c1 = C.new()
3785 var c2 = C.new() 3848 var c2 = C.new()
3786 END 3849 END
3792 def Test_lockvar_object() 3855 def Test_lockvar_object()
3793 var lines =<< trim END 3856 var lines =<< trim END
3794 vim9script 3857 vim9script
3795 3858
3796 class C 3859 class C
3797 this.val: number 3860 var val: number
3798 def new(this.val) 3861 def new(this.val)
3799 enddef 3862 enddef
3800 endclass 3863 endclass
3801 3864
3802 var some_dict: dict<C> = { a: C.new(1), b: C.new(2), c: C.new(3), } 3865 var some_dict: dict<C> = { a: C.new(1), b: C.new(2), c: C.new(3), }
3838 # read-only lockvar from object method 3901 # read-only lockvar from object method
3839 var lines =<< trim END 3902 var lines =<< trim END
3840 vim9script 3903 vim9script
3841 3904
3842 class C 3905 class C
3843 this.val1: number 3906 var val1: number
3844 def Lock() 3907 def Lock()
3845 lockvar this.val1 3908 lockvar this.val1
3846 enddef 3909 enddef
3847 endclass 3910 endclass
3848 var o = C.new(3) 3911 var o = C.new(3)
3853 # read-only lockvar from scriptlevel 3916 # read-only lockvar from scriptlevel
3854 lines =<< trim END 3917 lines =<< trim END
3855 vim9script 3918 vim9script
3856 3919
3857 class C 3920 class C
3858 this.val2: number 3921 var val2: number
3859 endclass 3922 endclass
3860 var o = C.new(3) 3923 var o = C.new(3)
3861 lockvar o.val2 3924 lockvar o.val2
3862 END 3925 END
3863 v9.CheckSourceFailure(lines, 'E1335: Variable "val2" in class "C" is not writable') 3926 v9.CheckSourceFailure(lines, 'E1335: Variable "val2" in class "C" is not writable')
3865 # read-only lockvar of scriptlevel variable from def 3928 # read-only lockvar of scriptlevel variable from def
3866 lines =<< trim END 3929 lines =<< trim END
3867 vim9script 3930 vim9script
3868 3931
3869 class C 3932 class C
3870 this.val3: number 3933 var val3: number
3871 endclass 3934 endclass
3872 var o = C.new(3) 3935 var o = C.new(3)
3873 def Lock() 3936 def Lock()
3874 lockvar o.val3 3937 lockvar o.val3
3875 enddef 3938 enddef
3880 # read-only lockvar of def argument variable 3943 # read-only lockvar of def argument variable
3881 lines =<< trim END 3944 lines =<< trim END
3882 vim9script 3945 vim9script
3883 3946
3884 class C 3947 class C
3885 this.val4: number 3948 var val4: number
3886 endclass 3949 endclass
3887 def Lock(o: C) 3950 def Lock(o: C)
3888 lockvar o.val4 3951 lockvar o.val4
3889 enddef 3952 enddef
3890 Lock(C.new(3)) 3953 Lock(C.new(3))
3897 # read-only lockvar from object method arg 3960 # read-only lockvar from object method arg
3898 lines =<< trim END 3961 lines =<< trim END
3899 vim9script 3962 vim9script
3900 3963
3901 class C 3964 class C
3902 this.val5: number 3965 var val5: number
3903 def Lock(o_any: any) 3966 def Lock(o_any: any)
3904 lockvar o_any.val5 3967 lockvar o_any.val5
3905 enddef 3968 enddef
3906 endclass 3969 endclass
3907 var o = C.new(3) 3970 var o = C.new(3)
3912 # read-only lockvar from class method arg 3975 # read-only lockvar from class method arg
3913 lines =<< trim END 3976 lines =<< trim END
3914 vim9script 3977 vim9script
3915 3978
3916 class C 3979 class C
3917 this.val6: number 3980 var val6: number
3918 static def Lock(o_any: any) 3981 static def Lock(o_any: any)
3919 lockvar o_any.val6 3982 lockvar o_any.val6
3920 enddef 3983 enddef
3921 endclass 3984 endclass
3922 var o = C.new(3) 3985 var o = C.new(3)
3931 # lockvar from object method 3994 # lockvar from object method
3932 lines =<< trim END 3995 lines =<< trim END
3933 vim9script 3996 vim9script
3934 3997
3935 class C 3998 class C
3936 public this.val1: number 3999 public var val1: number
3937 def Lock() 4000 def Lock()
3938 lockvar this.val1 4001 lockvar this.val1
3939 enddef 4002 enddef
3940 endclass 4003 endclass
3941 var o = C.new(3) 4004 var o = C.new(3)
3946 # lockvar from scriptlevel 4009 # lockvar from scriptlevel
3947 lines =<< trim END 4010 lines =<< trim END
3948 vim9script 4011 vim9script
3949 4012
3950 class C 4013 class C
3951 public this.val2: number 4014 public var val2: number
3952 endclass 4015 endclass
3953 var o = C.new(3) 4016 var o = C.new(3)
3954 lockvar o.val2 4017 lockvar o.val2
3955 END 4018 END
3956 v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "o.val2" in class "C"', 7) 4019 v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "o.val2" in class "C"', 7)
3958 # lockvar of scriptlevel variable from def 4021 # lockvar of scriptlevel variable from def
3959 lines =<< trim END 4022 lines =<< trim END
3960 vim9script 4023 vim9script
3961 4024
3962 class C 4025 class C
3963 public this.val3: number 4026 public var val3: number
3964 endclass 4027 endclass
3965 var o = C.new(3) 4028 var o = C.new(3)
3966 def Lock() 4029 def Lock()
3967 lockvar o.val3 4030 lockvar o.val3
3968 enddef 4031 enddef
3973 # lockvar of def argument variable 4036 # lockvar of def argument variable
3974 lines =<< trim END 4037 lines =<< trim END
3975 vim9script 4038 vim9script
3976 4039
3977 class C 4040 class C
3978 public this.val4: number 4041 public var val4: number
3979 endclass 4042 endclass
3980 def Lock(o: C) 4043 def Lock(o: C)
3981 lockvar o.val4 4044 lockvar o.val4
3982 enddef 4045 enddef
3983 Lock(C.new(3)) 4046 Lock(C.new(3))
3987 # lockvar from object method arg 4050 # lockvar from object method arg
3988 lines =<< trim END 4051 lines =<< trim END
3989 vim9script 4052 vim9script
3990 4053
3991 class C 4054 class C
3992 public this.val5: number 4055 public var val5: number
3993 def Lock(o_any: any) 4056 def Lock(o_any: any)
3994 lockvar o_any.val5 4057 lockvar o_any.val5
3995 enddef 4058 enddef
3996 endclass 4059 endclass
3997 var o = C.new(3) 4060 var o = C.new(3)
4002 # lockvar from class method arg 4065 # lockvar from class method arg
4003 lines =<< trim END 4066 lines =<< trim END
4004 vim9script 4067 vim9script
4005 4068
4006 class C 4069 class C
4007 public this.val6: number 4070 public var val6: number
4008 static def Lock(o_any: any) 4071 static def Lock(o_any: any)
4009 lockvar o_any.val6 4072 lockvar o_any.val6
4010 enddef 4073 enddef
4011 endclass 4074 endclass
4012 var o = C.new(3) 4075 var o = C.new(3)
4021 # lockvar bare static from object method 4084 # lockvar bare static from object method
4022 var lines =<< trim END 4085 var lines =<< trim END
4023 vim9script 4086 vim9script
4024 4087
4025 class C 4088 class C
4026 public static sval1: number 4089 public static var sval1: number
4027 def Lock() 4090 def Lock()
4028 lockvar sval1 4091 lockvar sval1
4029 enddef 4092 enddef
4030 endclass 4093 endclass
4031 var o = C.new() 4094 var o = C.new()
4036 # lockvar C.static from object method 4099 # lockvar C.static from object method
4037 lines =<< trim END 4100 lines =<< trim END
4038 vim9script 4101 vim9script
4039 4102
4040 class C 4103 class C
4041 public static sval2: number 4104 public static var sval2: number
4042 def Lock() 4105 def Lock()
4043 lockvar C.sval2 4106 lockvar C.sval2
4044 enddef 4107 enddef
4045 endclass 4108 endclass
4046 var o = C.new() 4109 var o = C.new()
4051 # lockvar bare static from class method 4114 # lockvar bare static from class method
4052 lines =<< trim END 4115 lines =<< trim END
4053 vim9script 4116 vim9script
4054 4117
4055 class C 4118 class C
4056 public static sval3: number 4119 public static var sval3: number
4057 static def Lock() 4120 static def Lock()
4058 lockvar sval3 4121 lockvar sval3
4059 enddef 4122 enddef
4060 endclass 4123 endclass
4061 C.Lock() 4124 C.Lock()
4065 # lockvar C.static from class method 4128 # lockvar C.static from class method
4066 lines =<< trim END 4129 lines =<< trim END
4067 vim9script 4130 vim9script
4068 4131
4069 class C 4132 class C
4070 public static sval4: number 4133 public static var sval4: number
4071 static def Lock() 4134 static def Lock()
4072 lockvar C.sval4 4135 lockvar C.sval4
4073 enddef 4136 enddef
4074 endclass 4137 endclass
4075 C.Lock() 4138 C.Lock()
4079 # lockvar C.static from script level 4142 # lockvar C.static from script level
4080 lines =<< trim END 4143 lines =<< trim END
4081 vim9script 4144 vim9script
4082 4145
4083 class C 4146 class C
4084 public static sval5: number 4147 public static var sval5: number
4085 endclass 4148 endclass
4086 lockvar C.sval5 4149 lockvar C.sval5
4087 END 4150 END
4088 v9.CheckSourceFailure(lines, 'E1392: Cannot (un)lock class variable "C.sval5" in class "C"', 6) 4151 v9.CheckSourceFailure(lines, 'E1392: Cannot (un)lock class variable "C.sval5" in class "C"', 6)
4089 4152
4090 # lockvar o.static from script level 4153 # lockvar o.static from script level
4091 lines =<< trim END 4154 lines =<< trim END
4092 vim9script 4155 vim9script
4093 4156
4094 class C 4157 class C
4095 public static sval6: number 4158 public static var sval6: number
4096 endclass 4159 endclass
4097 var o = C.new() 4160 var o = C.new()
4098 lockvar o.sval6 4161 lockvar o.sval6
4099 END 4162 END
4100 v9.CheckSourceFailure(lines, 'E1375: Class variable "sval6" accessible only using class "C"', 7) 4163 v9.CheckSourceFailure(lines, 'E1375: Class variable "sval6" accessible only using class "C"', 7)
4121 # class member in "C". This tests lval_root_is_arg. 4184 # class member in "C". This tests lval_root_is_arg.
4122 lines =<< trim END 4185 lines =<< trim END
4123 vim9script 4186 vim9script
4124 4187
4125 class C 4188 class C
4126 public static sval: list<number> 4189 public static var sval: list<number>
4127 endclass 4190 endclass
4128 4191
4129 def Lock2(sval: any) 4192 def Lock2(sval: any)
4130 lockvar sval 4193 lockvar sval
4131 enddef 4194 enddef
4138 # Lock a class. 4201 # Lock a class.
4139 lines =<< trim END 4202 lines =<< trim END
4140 vim9script 4203 vim9script
4141 4204
4142 class C 4205 class C
4143 public static sval: list<number> 4206 public static var sval: list<number>
4144 endclass 4207 endclass
4145 4208
4146 def Lock2(sval: any) 4209 def Lock2(sval: any)
4147 lockvar sval 4210 lockvar sval
4148 enddef 4211 enddef
4154 # Lock an object. 4217 # Lock an object.
4155 lines =<< trim END 4218 lines =<< trim END
4156 vim9script 4219 vim9script
4157 4220
4158 class C 4221 class C
4159 public static sval: list<number> 4222 public static var sval: list<number>
4160 endclass 4223 endclass
4161 4224
4162 def Lock2(sval: any) 4225 def Lock2(sval: any)
4163 lockvar sval 4226 lockvar sval
4164 enddef 4227 enddef
4170 # In this case (unlike previous) "lockvar sval" is a class member. 4233 # In this case (unlike previous) "lockvar sval" is a class member.
4171 lines =<< trim END 4234 lines =<< trim END
4172 vim9script 4235 vim9script
4173 4236
4174 class C 4237 class C
4175 public static sval: list<number> 4238 public static var sval: list<number>
4176 def Lock2() 4239 def Lock2()
4177 lockvar sval 4240 lockvar sval
4178 enddef 4241 enddef
4179 endclass 4242 endclass
4180 4243
4245 # Lock a list element that's nested in an object variable from a :def 4308 # Lock a list element that's nested in an object variable from a :def
4246 lines =<< trim END 4309 lines =<< trim END
4247 vim9script 4310 vim9script
4248 4311
4249 class C 4312 class C
4250 public this.val: list<list<number>> = [ [1], [2], [3] ] 4313 public var val: list<list<number>> = [ [1], [2], [3] ]
4251 endclass 4314 endclass
4252 def Lock2(obj: any) 4315 def Lock2(obj: any)
4253 lockvar obj.val[1] 4316 lockvar obj.val[1]
4254 enddef 4317 enddef
4255 4318
4271 # Lock a list element that's nested in an object variable from scriptlevel 4334 # Lock a list element that's nested in an object variable from scriptlevel
4272 lines =<< trim END 4335 lines =<< trim END
4273 vim9script 4336 vim9script
4274 4337
4275 class C 4338 class C
4276 public this.val: list<list<number>> = [ [1], [2], [3] ] 4339 public var val: list<list<number>> = [ [1], [2], [3] ]
4277 endclass 4340 endclass
4278 4341
4279 var o = C.new() 4342 var o = C.new()
4280 lockvar o.val[1] 4343 lockvar o.val[1]
4281 o.val[0] = [9] 4344 o.val[0] = [9]
4311 # in an object fetched via a script level list 4374 # in an object fetched via a script level list
4312 lines =<< trim END 4375 lines =<< trim END
4313 vim9script 4376 vim9script
4314 4377
4315 class C 4378 class C
4316 this._v1: list<list<number>> 4379 var _v1: list<list<number>>
4317 def Lock() 4380 def Lock()
4318 lockvar lc[0]._v1[1] 4381 lockvar lc[0]._v1[1]
4319 enddef 4382 enddef
4320 endclass 4383 endclass
4321 4384
4335 # the same name. 4398 # the same name.
4336 lines =<< trim END 4399 lines =<< trim END
4337 vim9script 4400 vim9script
4338 4401
4339 class C2 4402 class C2
4340 this._v1: list<list<number>> 4403 var _v1: list<list<number>>
4341 def Lock(obj: any) 4404 def Lock(obj: any)
4342 lockvar lc[0]._v1[1] 4405 lockvar lc[0]._v1[1]
4343 enddef 4406 enddef
4344 endclass 4407 endclass
4345 4408
4346 class C 4409 class C
4347 this._v1: list<list<number>> 4410 var _v1: list<list<number>>
4348 endclass 4411 endclass
4349 4412
4350 var l = [[1], [2], [3]] 4413 var l = [[1], [2], [3]]
4351 var o = C.new(l) 4414 var o = C.new(l)
4352 var lc: list<C> = [ o ] 4415 var lc: list<C> = [ o ]
4365 # variable is at index 1 within class/object 4428 # variable is at index 1 within class/object
4366 var lines =<< trim END 4429 var lines =<< trim END
4367 vim9script 4430 vim9script
4368 4431
4369 class C 4432 class C
4370 this.o0: list<list<number>> = [ [0], [1], [2]] 4433 var o0: list<list<number>> = [ [0], [1], [2]]
4371 this.o1: list<list<number>> = [[10], [11], [12]] 4434 var o1: list<list<number>> = [[10], [11], [12]]
4372 static c0: list<list<number>> = [[20], [21], [22]] 4435 static var c0: list<list<number>> = [[20], [21], [22]]
4373 static c1: list<list<number>> = [[30], [31], [32]] 4436 static var c1: list<list<number>> = [[30], [31], [32]]
4374 endclass 4437 endclass
4375 4438
4376 def LockIt(arg: any) 4439 def LockIt(arg: any)
4377 lockvar arg 4440 lockvar arg
4378 enddef 4441 enddef
4443 var l0o1 = [ [10], [11], [12]] 4506 var l0o1 = [ [10], [11], [12]]
4444 var l0c0 = [[120], [121], [122]] 4507 var l0c0 = [[120], [121], [122]]
4445 var l0c1 = [[130], [131], [132]] 4508 var l0c1 = [[130], [131], [132]]
4446 4509
4447 class C0 4510 class C0
4448 this.o0: list<list<number>> = l0o0 4511 var o0: list<list<number>> = l0o0
4449 this.o1: list<list<number>> = l0o1 4512 var o1: list<list<number>> = l0o1
4450 static c0: list<list<number>> = l0c0 4513 static var c0: list<list<number>> = l0c0
4451 static c1: list<list<number>> = l0c1 4514 static var c1: list<list<number>> = l0c1
4452 def Islocked(arg: string): number 4515 def Islocked(arg: string): number
4453 return islocked(arg) 4516 return islocked(arg)
4454 enddef 4517 enddef
4455 static def SIslocked(arg: string): number 4518 static def SIslocked(arg: string): number
4456 return islocked(arg) 4519 return islocked(arg)
4461 var l2o1 = [[20010], [20011], [20012]] 4524 var l2o1 = [[20010], [20011], [20012]]
4462 var l2c0 = [[20120], [20121], [20122]] 4525 var l2c0 = [[20120], [20121], [20122]]
4463 var l2c1 = [[20130], [20131], [20132]] 4526 var l2c1 = [[20130], [20131], [20132]]
4464 4527
4465 class C2 4528 class C2
4466 this.o0: list<list<number>> = l2o0 4529 var o0: list<list<number>> = l2o0
4467 this.o1: list<list<number>> = l2o1 4530 var o1: list<list<number>> = l2o1
4468 static c0: list<list<number>> = l2c0 4531 static var c0: list<list<number>> = l2c0
4469 static c1: list<list<number>> = l2c1 4532 static var c1: list<list<number>> = l2c1
4470 def Islocked(arg: string): number 4533 def Islocked(arg: string): number
4471 return islocked(arg) 4534 return islocked(arg)
4472 enddef 4535 enddef
4473 static def SIslocked(arg: string): number 4536 static def SIslocked(arg: string): number
4474 return islocked(arg) 4537 return islocked(arg)
4630 4693
4631 lines =<< trim END 4694 lines =<< trim END
4632 vim9script 4695 vim9script
4633 4696
4634 class C 4697 class C
4635 this.val = { key: "value" } 4698 var val = { key: "value" }
4636 def Islocked(arg: string): number 4699 def Islocked(arg: string): number
4637 return islocked(arg) 4700 return islocked(arg)
4638 enddef 4701 enddef
4639 endclass 4702 endclass
4640 var obj = C.new() 4703 var obj = C.new()
5167 # subclasses get their own static copy 5230 # subclasses get their own static copy
5168 var lines =<< trim END 5231 var lines =<< trim END
5169 vim9script 5232 vim9script
5170 5233
5171 class A 5234 class A
5172 static _svar: number 5235 static var _svar: number
5173 this._mvar: number 5236 var _mvar: number
5174 def new() 5237 def new()
5175 _svar = 1 5238 _svar = 1
5176 this._mvar = 101 5239 this._mvar = 101
5177 enddef 5240 enddef
5178 def AccessObject(): number 5241 def AccessObject(): number
5221 def Test_dup_member_variable() 5284 def Test_dup_member_variable()
5222 # Duplicate member variable 5285 # Duplicate member variable
5223 var lines =<< trim END 5286 var lines =<< trim END
5224 vim9script 5287 vim9script
5225 class C 5288 class C
5226 this.val = 10 5289 var val = 10
5227 this.val = 20 5290 var val = 20
5228 endclass 5291 endclass
5229 END 5292 END
5230 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4) 5293 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4)
5231 5294
5232 # Duplicate protected member variable 5295 # Duplicate protected member variable
5233 lines =<< trim END 5296 lines =<< trim END
5234 vim9script 5297 vim9script
5235 class C 5298 class C
5236 this._val = 10 5299 var _val = 10
5237 this._val = 20 5300 var _val = 20
5238 endclass 5301 endclass
5239 END 5302 END
5240 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 4) 5303 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 4)
5241 5304
5242 # Duplicate public member variable 5305 # Duplicate public member variable
5243 lines =<< trim END 5306 lines =<< trim END
5244 vim9script 5307 vim9script
5245 class C 5308 class C
5246 public this.val = 10 5309 public var val = 10
5247 public this.val = 20 5310 public var val = 20
5248 endclass 5311 endclass
5249 END 5312 END
5250 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4) 5313 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4)
5251 5314
5252 # Duplicate protected member variable 5315 # Duplicate protected member variable
5253 lines =<< trim END 5316 lines =<< trim END
5254 vim9script 5317 vim9script
5255 class C 5318 class C
5256 this.val = 10 5319 var val = 10
5257 this._val = 20 5320 var _val = 20
5258 endclass 5321 endclass
5259 END 5322 END
5260 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 4) 5323 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 4)
5261 5324
5262 # Duplicate public and protected member variable 5325 # Duplicate public and protected member variable
5263 lines =<< trim END 5326 lines =<< trim END
5264 vim9script 5327 vim9script
5265 class C 5328 class C
5266 this._val = 20 5329 var _val = 20
5267 public this.val = 10 5330 public var val = 10
5268 endclass 5331 endclass
5269 END 5332 END
5270 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4) 5333 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 4)
5271 5334
5272 # Duplicate class member variable 5335 # Duplicate class member variable
5273 lines =<< trim END 5336 lines =<< trim END
5274 vim9script 5337 vim9script
5275 class C 5338 class C
5276 static s: string = "abc" 5339 static var s: string = "abc"
5277 static _s: string = "def" 5340 static var _s: string = "def"
5278 endclass 5341 endclass
5279 END 5342 END
5280 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _s', 4) 5343 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _s', 4)
5281 5344
5282 # Duplicate public and protected class member variable 5345 # Duplicate public and protected class member variable
5283 lines =<< trim END 5346 lines =<< trim END
5284 vim9script 5347 vim9script
5285 class C 5348 class C
5286 public static s: string = "abc" 5349 public static var s: string = "abc"
5287 static _s: string = "def" 5350 static var _s: string = "def"
5288 endclass 5351 endclass
5289 END 5352 END
5290 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _s', 4) 5353 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _s', 4)
5291 5354
5292 # Duplicate class and object member variable 5355 # Duplicate class and object member variable
5293 lines =<< trim END 5356 lines =<< trim END
5294 vim9script 5357 vim9script
5295 class C 5358 class C
5296 static val = 10 5359 static var val = 10
5297 this.val = 20 5360 var val = 20
5298 def new() 5361 def new()
5299 enddef 5362 enddef
5300 endclass 5363 endclass
5301 var c = C.new() 5364 var c = C.new()
5302 assert_equal(10, C.val) 5365 assert_equal(10, C.val)
5306 5369
5307 # Duplicate object member variable in a derived class 5370 # Duplicate object member variable in a derived class
5308 lines =<< trim END 5371 lines =<< trim END
5309 vim9script 5372 vim9script
5310 class A 5373 class A
5311 this.val = 10 5374 var val = 10
5312 endclass 5375 endclass
5313 class B extends A 5376 class B extends A
5314 endclass 5377 endclass
5315 class C extends B 5378 class C extends B
5316 this.val = 20 5379 var val = 20
5317 endclass 5380 endclass
5318 END 5381 END
5319 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 9) 5382 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 9)
5320 5383
5321 # Duplicate object protected member variable in a derived class 5384 # Duplicate object protected member variable in a derived class
5322 lines =<< trim END 5385 lines =<< trim END
5323 vim9script 5386 vim9script
5324 class A 5387 class A
5325 this._val = 10 5388 var _val = 10
5326 endclass 5389 endclass
5327 class B extends A 5390 class B extends A
5328 endclass 5391 endclass
5329 class C extends B 5392 class C extends B
5330 this._val = 20 5393 var _val = 20
5331 endclass 5394 endclass
5332 END 5395 END
5333 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 9) 5396 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 9)
5334 5397
5335 # Duplicate object protected member variable in a derived class 5398 # Duplicate object protected member variable in a derived class
5336 lines =<< trim END 5399 lines =<< trim END
5337 vim9script 5400 vim9script
5338 class A 5401 class A
5339 this.val = 10 5402 var val = 10
5340 endclass 5403 endclass
5341 class B extends A 5404 class B extends A
5342 endclass 5405 endclass
5343 class C extends B 5406 class C extends B
5344 this._val = 20 5407 var _val = 20
5345 endclass 5408 endclass
5346 END 5409 END
5347 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 9) 5410 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: _val', 9)
5348 5411
5349 # Duplicate object member variable in a derived class 5412 # Duplicate object member variable in a derived class
5350 lines =<< trim END 5413 lines =<< trim END
5351 vim9script 5414 vim9script
5352 class A 5415 class A
5353 this._val = 10 5416 var _val = 10
5354 endclass 5417 endclass
5355 class B extends A 5418 class B extends A
5356 endclass 5419 endclass
5357 class C extends B 5420 class C extends B
5358 this.val = 20 5421 var val = 20
5359 endclass 5422 endclass
5360 END 5423 END
5361 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 9) 5424 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: val', 9)
5362 5425
5363 # Two member variables with a common prefix 5426 # Two member variables with a common prefix
5364 lines =<< trim END 5427 lines =<< trim END
5365 vim9script 5428 vim9script
5366 class A 5429 class A
5367 public static svar2: number 5430 public static var svar2: number
5368 public static svar: number 5431 public static var svar: number
5369 endclass 5432 endclass
5370 END 5433 END
5371 v9.CheckSourceSuccess(lines) 5434 v9.CheckSourceSuccess(lines)
5372 enddef 5435 enddef
5373 5436
5375 def Test_private_member_access_outside_class() 5438 def Test_private_member_access_outside_class()
5376 # protected object member variable 5439 # protected object member variable
5377 var lines =<< trim END 5440 var lines =<< trim END
5378 vim9script 5441 vim9script
5379 class A 5442 class A
5380 this._val = 10 5443 var _val = 10
5381 def GetVal(): number 5444 def GetVal(): number
5382 return this._val 5445 return this._val
5383 enddef 5446 enddef
5384 endclass 5447 endclass
5385 def T() 5448 def T()
5392 5455
5393 # access a non-existing protected object member variable 5456 # access a non-existing protected object member variable
5394 lines =<< trim END 5457 lines =<< trim END
5395 vim9script 5458 vim9script
5396 class A 5459 class A
5397 this._val = 10 5460 var _val = 10
5398 endclass 5461 endclass
5399 def T() 5462 def T()
5400 var a = A.new() 5463 var a = A.new()
5401 a._a = 1 5464 a._a = 1
5402 enddef 5465 enddef
5406 5469
5407 # protected static member variable 5470 # protected static member variable
5408 lines =<< trim END 5471 lines =<< trim END
5409 vim9script 5472 vim9script
5410 class A 5473 class A
5411 static _val = 10 5474 static var _val = 10
5412 endclass 5475 endclass
5413 def T() 5476 def T()
5414 var a = A.new() 5477 var a = A.new()
5415 var x = a._val 5478 var x = a._val
5416 enddef 5479 enddef
5420 5483
5421 # protected static member variable 5484 # protected static member variable
5422 lines =<< trim END 5485 lines =<< trim END
5423 vim9script 5486 vim9script
5424 class A 5487 class A
5425 static _val = 10 5488 static var _val = 10
5426 endclass 5489 endclass
5427 def T() 5490 def T()
5428 var a = A.new() 5491 var a = A.new()
5429 a._val = 3 5492 a._val = 3
5430 enddef 5493 enddef
5434 5497
5435 # protected static class variable 5498 # protected static class variable
5436 lines =<< trim END 5499 lines =<< trim END
5437 vim9script 5500 vim9script
5438 class A 5501 class A
5439 static _val = 10 5502 static var _val = 10
5440 endclass 5503 endclass
5441 def T() 5504 def T()
5442 var x = A._val 5505 var x = A._val
5443 enddef 5506 enddef
5444 T() 5507 T()
5447 5510
5448 # protected static class variable 5511 # protected static class variable
5449 lines =<< trim END 5512 lines =<< trim END
5450 vim9script 5513 vim9script
5451 class A 5514 class A
5452 static _val = 10 5515 static var _val = 10
5453 endclass 5516 endclass
5454 def T() 5517 def T()
5455 A._val = 3 5518 A._val = 3
5456 enddef 5519 enddef
5457 T() 5520 T()
5462 " Test for changing the member access of an interface in a implementation class 5525 " Test for changing the member access of an interface in a implementation class
5463 def Test_change_interface_member_access() 5526 def Test_change_interface_member_access()
5464 var lines =<< trim END 5527 var lines =<< trim END
5465 vim9script 5528 vim9script
5466 interface A 5529 interface A
5467 this.val: number 5530 var val: number
5468 endinterface 5531 endinterface
5469 class B implements A 5532 class B implements A
5470 public this.val = 10 5533 public var val = 10
5471 endclass 5534 endclass
5472 END 5535 END
5473 v9.CheckSourceFailure(lines, 'E1367: Access level of variable "val" of interface "A" is different', 7) 5536 v9.CheckSourceFailure(lines, 'E1367: Access level of variable "val" of interface "A" is different', 7)
5474 5537
5475 lines =<< trim END 5538 lines =<< trim END
5476 vim9script 5539 vim9script
5477 interface A 5540 interface A
5478 this.val: number 5541 var val: number
5479 endinterface 5542 endinterface
5480 class B implements A 5543 class B implements A
5481 public this.val = 10 5544 public var val = 10
5482 endclass 5545 endclass
5483 END 5546 END
5484 v9.CheckSourceFailure(lines, 'E1367: Access level of variable "val" of interface "A" is different', 7) 5547 v9.CheckSourceFailure(lines, 'E1367: Access level of variable "val" of interface "A" is different', 7)
5485 enddef 5548 enddef
5486 5549
5487 " Test for trying to change a readonly member from a def function 5550 " Test for trying to change a readonly member from a def function
5488 def Test_readonly_member_change_in_def_func() 5551 def Test_readonly_member_change_in_def_func()
5489 var lines =<< trim END 5552 var lines =<< trim END
5490 vim9script 5553 vim9script
5491 class A 5554 class A
5492 this.val: number 5555 var val: number
5493 endclass 5556 endclass
5494 def T() 5557 def T()
5495 var a = A.new() 5558 var a = A.new()
5496 a.val = 20 5559 a.val = 20
5497 enddef 5560 enddef
5503 " Test for reading and writing a class member from a def function 5566 " Test for reading and writing a class member from a def function
5504 def Test_modify_class_member_from_def_function() 5567 def Test_modify_class_member_from_def_function()
5505 var lines =<< trim END 5568 var lines =<< trim END
5506 vim9script 5569 vim9script
5507 class A 5570 class A
5508 this.var1: number = 10 5571 var var1: number = 10
5509 public static var2: list<number> = [1, 2] 5572 public static var var2: list<number> = [1, 2]
5510 public static var3: dict<number> = {a: 1, b: 2} 5573 public static var var3: dict<number> = {a: 1, b: 2}
5511 static _priv_var4: number = 40 5574 static var _priv_var4: number = 40
5512 endclass 5575 endclass
5513 def T() 5576 def T()
5514 assert_equal([1, 2], A.var2) 5577 assert_equal([1, 2], A.var2)
5515 assert_equal({a: 1, b: 2}, A.var3) 5578 assert_equal({a: 1, b: 2}, A.var3)
5516 A.var2 = [3, 4] 5579 A.var2 = [3, 4]
5527 " Test for accessing a class member variable using an object 5590 " Test for accessing a class member variable using an object
5528 def Test_class_variable_access_using_object() 5591 def Test_class_variable_access_using_object()
5529 var lines =<< trim END 5592 var lines =<< trim END
5530 vim9script 5593 vim9script
5531 class A 5594 class A
5532 public static svar1: list<number> = [1] 5595 public static var svar1: list<number> = [1]
5533 public static svar2: list<number> = [2] 5596 public static var svar2: list<number> = [2]
5534 endclass 5597 endclass
5535 5598
5536 A.svar1->add(3) 5599 A.svar1->add(3)
5537 A.svar2->add(4) 5600 A.svar2->add(4)
5538 assert_equal([1, 3], A.svar1) 5601 assert_equal([1, 3], A.svar1)
5550 5613
5551 # Cannot read from a class variable using an object in script context 5614 # Cannot read from a class variable using an object in script context
5552 lines =<< trim END 5615 lines =<< trim END
5553 vim9script 5616 vim9script
5554 class A 5617 class A
5555 public this.var1: number 5618 public var var1: number
5556 public static svar2: list<number> = [1] 5619 public static var svar2: list<number> = [1]
5557 endclass 5620 endclass
5558 5621
5559 var a = A.new() 5622 var a = A.new()
5560 echo a.svar2 5623 echo a.svar2
5561 END 5624 END
5563 5626
5564 # Cannot write to a class variable using an object in script context 5627 # Cannot write to a class variable using an object in script context
5565 lines =<< trim END 5628 lines =<< trim END
5566 vim9script 5629 vim9script
5567 class A 5630 class A
5568 public this.var1: number 5631 public var var1: number
5569 public static svar2: list<number> = [1] 5632 public static var svar2: list<number> = [1]
5570 endclass 5633 endclass
5571 5634
5572 var a = A.new() 5635 var a = A.new()
5573 a.svar2 = [2] 5636 a.svar2 = [2]
5574 END 5637 END
5576 5639
5577 # Cannot read from a class variable using an object in def method context 5640 # Cannot read from a class variable using an object in def method context
5578 lines =<< trim END 5641 lines =<< trim END
5579 vim9script 5642 vim9script
5580 class A 5643 class A
5581 public this.var1: number 5644 public var var1: number
5582 public static svar2: list<number> = [1] 5645 public static var svar2: list<number> = [1]
5583 endclass 5646 endclass
5584 5647
5585 def T() 5648 def T()
5586 var a = A.new() 5649 var a = A.new()
5587 echo a.svar2 5650 echo a.svar2
5592 5655
5593 # Cannot write to a class variable using an object in def method context 5656 # Cannot write to a class variable using an object in def method context
5594 lines =<< trim END 5657 lines =<< trim END
5595 vim9script 5658 vim9script
5596 class A 5659 class A
5597 public this.var1: number 5660 public var var1: number
5598 public static svar2: list<number> = [1] 5661 public static var svar2: list<number> = [1]
5599 endclass 5662 endclass
5600 5663
5601 def T() 5664 def T()
5602 var a = A.new() 5665 var a = A.new()
5603 a.svar2 = [2] 5666 a.svar2 = [2]
5924 def Test_class_variable() 5987 def Test_class_variable()
5925 var lines =<< trim END 5988 var lines =<< trim END
5926 vim9script 5989 vim9script
5927 5990
5928 class A 5991 class A
5929 public static val: number = 10 5992 public static var val: number = 10
5930 static def ClassFunc() 5993 static def ClassFunc()
5931 assert_equal(10, val) 5994 assert_equal(10, val)
5932 enddef 5995 enddef
5933 def ObjFunc() 5996 def ObjFunc()
5934 assert_equal(10, val) 5997 assert_equal(10, val)
5959 # Modifying a parent class variable from a child class method 6022 # Modifying a parent class variable from a child class method
5960 lines =<< trim END 6023 lines =<< trim END
5961 vim9script 6024 vim9script
5962 6025
5963 class A 6026 class A
5964 static val: number = 10 6027 static var val: number = 10
5965 endclass 6028 endclass
5966 6029
5967 class B extends A 6030 class B extends A
5968 static def ClassFunc() 6031 static def ClassFunc()
5969 val = 20 6032 val = 20
5976 # Reading a parent class variable from a child class method 6039 # Reading a parent class variable from a child class method
5977 lines =<< trim END 6040 lines =<< trim END
5978 vim9script 6041 vim9script
5979 6042
5980 class A 6043 class A
5981 static val: number = 10 6044 static var val: number = 10
5982 endclass 6045 endclass
5983 6046
5984 class B extends A 6047 class B extends A
5985 static def ClassFunc() 6048 static def ClassFunc()
5986 var i = val 6049 var i = val
5993 # Modifying a parent class variable from a child object method 6056 # Modifying a parent class variable from a child object method
5994 lines =<< trim END 6057 lines =<< trim END
5995 vim9script 6058 vim9script
5996 6059
5997 class A 6060 class A
5998 static val: number = 10 6061 static var val: number = 10
5999 endclass 6062 endclass
6000 6063
6001 class B extends A 6064 class B extends A
6002 def ObjFunc() 6065 def ObjFunc()
6003 val = 20 6066 val = 20
6011 # Reading a parent class variable from a child object method 6074 # Reading a parent class variable from a child object method
6012 lines =<< trim END 6075 lines =<< trim END
6013 vim9script 6076 vim9script
6014 6077
6015 class A 6078 class A
6016 static val: number = 10 6079 static var val: number = 10
6017 endclass 6080 endclass
6018 6081
6019 class B extends A 6082 class B extends A
6020 def ObjFunc() 6083 def ObjFunc()
6021 var i = val 6084 var i = val
6029 # Modifying a class variable using an object at script level 6092 # Modifying a class variable using an object at script level
6030 lines =<< trim END 6093 lines =<< trim END
6031 vim9script 6094 vim9script
6032 6095
6033 class A 6096 class A
6034 static val: number = 10 6097 static var val: number = 10
6035 endclass 6098 endclass
6036 var a = A.new() 6099 var a = A.new()
6037 a.val = 20 6100 a.val = 20
6038 END 6101 END
6039 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 7) 6102 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 7)
6041 # Reading a class variable using an object at script level 6104 # Reading a class variable using an object at script level
6042 lines =<< trim END 6105 lines =<< trim END
6043 vim9script 6106 vim9script
6044 6107
6045 class A 6108 class A
6046 static val: number = 10 6109 static var val: number = 10
6047 endclass 6110 endclass
6048 var a = A.new() 6111 var a = A.new()
6049 var i = a.val 6112 var i = a.val
6050 END 6113 END
6051 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 7) 6114 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 7)
6053 # Modifying a class variable using an object at function level 6116 # Modifying a class variable using an object at function level
6054 lines =<< trim END 6117 lines =<< trim END
6055 vim9script 6118 vim9script
6056 6119
6057 class A 6120 class A
6058 static val: number = 10 6121 static var val: number = 10
6059 endclass 6122 endclass
6060 6123
6061 def T() 6124 def T()
6062 var a = A.new() 6125 var a = A.new()
6063 a.val = 20 6126 a.val = 20
6069 # Reading a class variable using an object at function level 6132 # Reading a class variable using an object at function level
6070 lines =<< trim END 6133 lines =<< trim END
6071 vim9script 6134 vim9script
6072 6135
6073 class A 6136 class A
6074 static val: number = 10 6137 static var val: number = 10
6075 endclass 6138 endclass
6076 def T() 6139 def T()
6077 var a = A.new() 6140 var a = A.new()
6078 var i = a.val 6141 var i = a.val
6079 enddef 6142 enddef
6080 T() 6143 T()
6081 END 6144 END
6082 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 2) 6145 v9.CheckSourceFailure(lines, 'E1375: Class variable "val" accessible only using class "A"', 2)
6146
6147 # Use old implicit var declaration syntax (without initialization)
6148 lines =<< trim END
6149 vim9script
6150
6151 class A
6152 static val: number
6153 endclass
6154 END
6155 v9.CheckSourceFailure(lines, 'E1368: Static must be followed by "var" or "def"', 4)
6156
6157 # Use old implicit var declaration syntax (with initialization)
6158 lines =<< trim END
6159 vim9script
6160
6161 class A
6162 static val: number = 10
6163 endclass
6164 END
6165 v9.CheckSourceFailure(lines, 'E1368: Static must be followed by "var" or "def"', 4)
6166
6167 # Use old implicit var declaration syntax (type inferred)
6168 lines =<< trim END
6169 vim9script
6170
6171 class A
6172 static val = 10
6173 endclass
6174 END
6175 v9.CheckSourceFailure(lines, 'E1368: Static must be followed by "var" or "def"', 4)
6176
6177 # Missing ":var" in "var" class variable declaration (without initialization)
6178 lines =<< trim END
6179 vim9script
6180
6181 class A
6182 static var: number
6183 endclass
6184 END
6185 v9.CheckSourceFailure(lines, 'E1329: Invalid class variable declaration: static var: number', 4)
6186
6187 # Missing ":var" in "var" class variable declaration (with initialization)
6188 lines =<< trim END
6189 vim9script
6190
6191 class A
6192 static var: number = 10
6193 endclass
6194 END
6195 v9.CheckSourceFailure(lines, 'E1329: Invalid class variable declaration: static var: number = 10', 4)
6196
6197 # Missing ":var" in "var" class variable declaration (type inferred)
6198 lines =<< trim END
6199 vim9script
6200
6201 class A
6202 static var = 10
6203 endclass
6204 END
6205 v9.CheckSourceFailure(lines, 'E1329: Invalid class variable declaration: static var = 10', 4)
6206
6083 enddef 6207 enddef
6084 6208
6085 " Test for using a duplicate class method and class variable in a child class 6209 " Test for using a duplicate class method and class variable in a child class
6086 def Test_dup_class_member() 6210 def Test_dup_class_member()
6087 # duplicate class variable, class method and overridden object method 6211 # duplicate class variable, class method and overridden object method
6088 var lines =<< trim END 6212 var lines =<< trim END
6089 vim9script 6213 vim9script
6090 class A 6214 class A
6091 static sval = 100 6215 static var sval = 100
6092 static def Check() 6216 static def Check()
6093 assert_equal(100, sval) 6217 assert_equal(100, sval)
6094 enddef 6218 enddef
6095 def GetVal(): number 6219 def GetVal(): number
6096 return sval 6220 return sval
6097 enddef 6221 enddef
6098 endclass 6222 endclass
6099 6223
6100 class B extends A 6224 class B extends A
6101 static sval = 200 6225 static var sval = 200
6102 static def Check() 6226 static def Check()
6103 assert_equal(200, sval) 6227 assert_equal(200, sval)
6104 enddef 6228 enddef
6105 def GetVal(): number 6229 def GetVal(): number
6106 return sval 6230 return sval
6128 6252
6129 # duplicate class variable and class method 6253 # duplicate class variable and class method
6130 lines =<< trim END 6254 lines =<< trim END
6131 vim9script 6255 vim9script
6132 class A 6256 class A
6133 static sval = 100 6257 static var sval = 100
6134 static def Check() 6258 static def Check()
6135 assert_equal(100, sval) 6259 assert_equal(100, sval)
6136 enddef 6260 enddef
6137 def GetVal(): number 6261 def GetVal(): number
6138 return sval 6262 return sval
6139 enddef 6263 enddef
6140 endclass 6264 endclass
6141 6265
6142 class B extends A 6266 class B extends A
6143 static sval = 200 6267 static var sval = 200
6144 static def Check() 6268 static def Check()
6145 assert_equal(200, sval) 6269 assert_equal(200, sval)
6146 enddef 6270 enddef
6147 endclass 6271 endclass
6148 6272
6301 class A 6425 class A
6302 endclass 6426 endclass
6303 class B extends A 6427 class B extends A
6304 endclass 6428 endclass
6305 class C extends B 6429 class C extends B
6306 public static rw_class_var = 1 6430 public static var rw_class_var = 1
6307 public this.rw_obj_var = 2 6431 public var rw_obj_var = 2
6308 static def ClassMethod(): number 6432 static def ClassMethod(): number
6309 return 3 6433 return 3
6310 enddef 6434 enddef
6311 def ObjMethod(): number 6435 def ObjMethod(): number
6312 return 4 6436 return 4
6325 " variable or a protected method or a public variable 6449 " variable or a protected method or a public variable
6326 def Test_interface_with_unsupported_members() 6450 def Test_interface_with_unsupported_members()
6327 var lines =<< trim END 6451 var lines =<< trim END
6328 vim9script 6452 vim9script
6329 interface A 6453 interface A
6330 static num: number 6454 static var num: number
6331 endinterface 6455 endinterface
6332 END 6456 END
6333 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3) 6457 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3)
6334 6458
6335 lines =<< trim END 6459 lines =<< trim END
6336 vim9script 6460 vim9script
6337 interface A 6461 interface A
6338 static _num: number 6462 static var _num: number
6339 endinterface 6463 endinterface
6340 END 6464 END
6341 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3) 6465 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3)
6342 6466
6343 lines =<< trim END 6467 lines =<< trim END
6344 vim9script 6468 vim9script
6345 interface A 6469 interface A
6346 public static num: number 6470 public static var num: number
6347 endinterface 6471 endinterface
6348 END 6472 END
6349 v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3) 6473 v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3)
6350 6474
6351 lines =<< trim END 6475 lines =<< trim END
6352 vim9script 6476 vim9script
6353 interface A 6477 interface A
6354 public static num: number 6478 public static var num: number
6355 endinterface 6479 endinterface
6356 END 6480 END
6357 v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3) 6481 v9.CheckSourceFailure(lines, 'E1387: Public variable not supported in an interface', 3)
6358 6482
6359 lines =<< trim END 6483 lines =<< trim END
6360 vim9script 6484 vim9script
6361 interface A 6485 interface A
6362 static _num: number 6486 static var _num: number
6363 endinterface 6487 endinterface
6364 END 6488 END
6365 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3) 6489 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3)
6366 6490
6367 lines =<< trim END 6491 lines =<< trim END
6381 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3) 6505 v9.CheckSourceFailure(lines, 'E1378: Static member not supported in an interface', 3)
6382 6506
6383 lines =<< trim END 6507 lines =<< trim END
6384 vim9script 6508 vim9script
6385 interface A 6509 interface A
6386 this._Foo: list<string> 6510 var _Foo: list<string>
6387 endinterface 6511 endinterface
6388 END 6512 END
6389 v9.CheckSourceFailure(lines, 'E1379: Protected variable not supported in an interface', 3) 6513 v9.CheckSourceFailure(lines, 'E1379: Protected variable not supported in an interface', 3)
6390 6514
6391 lines =<< trim END 6515 lines =<< trim END
6400 " Test for extending an interface 6524 " Test for extending an interface
6401 def Test_extend_interface() 6525 def Test_extend_interface()
6402 var lines =<< trim END 6526 var lines =<< trim END
6403 vim9script 6527 vim9script
6404 interface A 6528 interface A
6405 this.var1: list<string> 6529 var var1: list<string>
6406 def Foo() 6530 def Foo()
6407 endinterface 6531 endinterface
6408 interface B extends A 6532 interface B extends A
6409 this.var2: dict<string> 6533 var var2: dict<string>
6410 def Bar() 6534 def Bar()
6411 endinterface 6535 endinterface
6412 class C implements A, B 6536 class C implements A, B
6413 this.var1 = [1, 2] 6537 var var1 = [1, 2]
6414 def Foo() 6538 def Foo()
6415 enddef 6539 enddef
6416 this.var2 = {a: '1'} 6540 var var2 = {a: '1'}
6417 def Bar() 6541 def Bar()
6418 enddef 6542 enddef
6419 endclass 6543 endclass
6420 END 6544 END
6421 v9.CheckSourceSuccess(lines) 6545 v9.CheckSourceSuccess(lines)
6436 vim9script 6560 vim9script
6437 interface A 6561 interface A
6438 def Foo() 6562 def Foo()
6439 endinterface 6563 endinterface
6440 interface B extends A 6564 interface B extends A
6441 this.var2: dict<string> 6565 var var2: dict<string>
6442 endinterface 6566 endinterface
6443 class C implements A, B 6567 class C implements A, B
6444 this.var2 = {a: '1'} 6568 var var2 = {a: '1'}
6445 endclass 6569 endclass
6446 END 6570 END
6447 v9.CheckSourceFailure(lines, 'E1349: Method "Foo" of interface "A" is not implemented', 10) 6571 v9.CheckSourceFailure(lines, 'E1349: Method "Foo" of interface "A" is not implemented', 10)
6448 6572
6449 lines =<< trim END 6573 lines =<< trim END
6450 vim9script 6574 vim9script
6451 interface A 6575 interface A
6452 def Foo() 6576 def Foo()
6453 endinterface 6577 endinterface
6454 interface B extends A 6578 interface B extends A
6455 this.var2: dict<string> 6579 var var2: dict<string>
6456 endinterface 6580 endinterface
6457 class C implements A, B 6581 class C implements A, B
6458 def Foo() 6582 def Foo()
6459 enddef 6583 enddef
6460 endclass 6584 endclass
6505 6629
6506 # Variable type in an extended interface is of different type 6630 # Variable type in an extended interface is of different type
6507 lines =<< trim END 6631 lines =<< trim END
6508 vim9script 6632 vim9script
6509 interface A 6633 interface A
6510 this.val1: number 6634 var val1: number
6511 endinterface 6635 endinterface
6512 interface B extends A 6636 interface B extends A
6513 this.val2: string 6637 var val2: string
6514 endinterface 6638 endinterface
6515 interface C extends B 6639 interface C extends B
6516 this.val1: string 6640 var val1: string
6517 this.val2: number 6641 var val2: number
6518 endinterface 6642 endinterface
6519 END 6643 END
6520 v9.CheckSourceFailure(lines, 'E1382: Variable "val1": type mismatch, expected number but got string', 11) 6644 v9.CheckSourceFailure(lines, 'E1382: Variable "val1": type mismatch, expected number but got string', 11)
6521 enddef 6645 enddef
6522 6646
6528 6652
6529 interface Intf 6653 interface Intf
6530 def F1(): list<list<number>> 6654 def F1(): list<list<number>>
6531 def F2(): list<list<number>> 6655 def F2(): list<list<number>>
6532 def F3(): list<list<number>> 6656 def F3(): list<list<number>>
6533 this.var1: list<dict<number>> 6657 var var1: list<dict<number>>
6534 this.var2: list<dict<number>> 6658 var var2: list<dict<number>>
6535 this.var3: list<dict<number>> 6659 var var3: list<dict<number>>
6536 endinterface 6660 endinterface
6537 6661
6538 class A 6662 class A
6539 def A1() 6663 def A1()
6540 enddef 6664 enddef
6541 def F3(): list<list<number>> 6665 def F3(): list<list<number>>
6542 return [[3]] 6666 return [[3]]
6543 enddef 6667 enddef
6544 this.v1: list<list<number>> = [[0]] 6668 var v1: list<list<number>> = [[0]]
6545 this.var3 = [{c: 30}] 6669 var var3 = [{c: 30}]
6546 endclass 6670 endclass
6547 6671
6548 class B extends A 6672 class B extends A
6549 def B1() 6673 def B1()
6550 enddef 6674 enddef
6551 def F2(): list<list<number>> 6675 def F2(): list<list<number>>
6552 return [[2]] 6676 return [[2]]
6553 enddef 6677 enddef
6554 this.v2: list<list<number>> = [[0]] 6678 var v2: list<list<number>> = [[0]]
6555 this.var2 = [{b: 20}] 6679 var var2 = [{b: 20}]
6556 endclass 6680 endclass
6557 6681
6558 class C extends B implements Intf 6682 class C extends B implements Intf
6559 def C1() 6683 def C1()
6560 enddef 6684 enddef
6561 def F1(): list<list<number>> 6685 def F1(): list<list<number>>
6562 return [[1]] 6686 return [[1]]
6563 enddef 6687 enddef
6564 this.v3: list<list<number>> = [[0]] 6688 var v3: list<list<number>> = [[0]]
6565 this.var1 = [{a: 10}] 6689 var var1 = [{a: 10}]
6566 endclass 6690 endclass
6567 6691
6568 def T(if: Intf) 6692 def T(if: Intf)
6569 assert_equal([[1]], if.F1()) 6693 assert_equal([[1]], if.F1())
6570 assert_equal([[2]], if.F2()) 6694 assert_equal([[2]], if.F2())
6653 # One of the interface variables is not present 6777 # One of the interface variables is not present
6654 lines =<< trim END 6778 lines =<< trim END
6655 vim9script 6779 vim9script
6656 6780
6657 interface Intf 6781 interface Intf
6658 this.var1: list<dict<number>> 6782 var var1: list<dict<number>>
6659 this.var2: list<dict<number>> 6783 var var2: list<dict<number>>
6660 this.var3: list<dict<number>> 6784 var var3: list<dict<number>>
6661 endinterface 6785 endinterface
6662 6786
6663 class A 6787 class A
6664 this.v1: list<list<number>> = [[0]] 6788 var v1: list<list<number>> = [[0]]
6665 endclass 6789 endclass
6666 6790
6667 class B extends A 6791 class B extends A
6668 this.v2: list<list<number>> = [[0]] 6792 var v2: list<list<number>> = [[0]]
6669 this.var2 = [{b: 20}] 6793 var var2 = [{b: 20}]
6670 endclass 6794 endclass
6671 6795
6672 class C extends B implements Intf 6796 class C extends B implements Intf
6673 this.v3: list<list<number>> = [[0]] 6797 var v3: list<list<number>> = [[0]]
6674 this.var1 = [{a: 10}] 6798 var var1 = [{a: 10}]
6675 endclass 6799 endclass
6676 END 6800 END
6677 v9.CheckSourceFailure(lines, 'E1348: Variable "var3" of interface "Intf" is not implemented', 21) 6801 v9.CheckSourceFailure(lines, 'E1348: Variable "var3" of interface "Intf" is not implemented', 21)
6678 6802
6679 # One of the interface variables is of different type 6803 # One of the interface variables is of different type
6680 lines =<< trim END 6804 lines =<< trim END
6681 vim9script 6805 vim9script
6682 6806
6683 interface Intf 6807 interface Intf
6684 this.var1: list<dict<number>> 6808 var var1: list<dict<number>>
6685 this.var2: list<dict<number>> 6809 var var2: list<dict<number>>
6686 this.var3: list<dict<number>> 6810 var var3: list<dict<number>>
6687 endinterface 6811 endinterface
6688 6812
6689 class A 6813 class A
6690 this.v1: list<list<number>> = [[0]] 6814 var v1: list<list<number>> = [[0]]
6691 this.var3: list<dict<string>> 6815 var var3: list<dict<string>>
6692 endclass 6816 endclass
6693 6817
6694 class B extends A 6818 class B extends A
6695 this.v2: list<list<number>> = [[0]] 6819 var v2: list<list<number>> = [[0]]
6696 this.var2 = [{b: 20}] 6820 var var2 = [{b: 20}]
6697 endclass 6821 endclass
6698 6822
6699 class C extends B implements Intf 6823 class C extends B implements Intf
6700 this.v3: list<list<number>> = [[0]] 6824 var v3: list<list<number>> = [[0]]
6701 this.var1 = [{a: 10}] 6825 var var1 = [{a: 10}]
6702 endclass 6826 endclass
6703 END 6827 END
6704 v9.CheckSourceFailure(lines, 'E1382: Variable "var3": type mismatch, expected list<dict<number>> but got list<dict<string>>', 22) 6828 v9.CheckSourceFailure(lines, 'E1382: Variable "var3": type mismatch, expected list<dict<number>> but got list<dict<string>>', 22)
6705 enddef 6829 enddef
6706 6830
6707 " Test for extending an interface with duplicate variables and methods 6831 " Test for extending an interface with duplicate variables and methods
6708 def Test_interface_extends_with_dup_members() 6832 def Test_interface_extends_with_dup_members()
6709 var lines =<< trim END 6833 var lines =<< trim END
6710 vim9script 6834 vim9script
6711 interface A 6835 interface A
6712 this.n1: number 6836 var n1: number
6713 def Foo1(): number 6837 def Foo1(): number
6714 endinterface 6838 endinterface
6715 interface B extends A 6839 interface B extends A
6716 this.n2: number 6840 var n2: number
6717 this.n1: number 6841 var n1: number
6718 def Foo2(): number 6842 def Foo2(): number
6719 def Foo1(): number 6843 def Foo1(): number
6720 endinterface 6844 endinterface
6721 class C implements B 6845 class C implements B
6722 this.n1 = 10 6846 var n1 = 10
6723 this.n2 = 20 6847 var n2 = 20
6724 def Foo1(): number 6848 def Foo1(): number
6725 return 30 6849 return 30
6726 enddef 6850 enddef
6727 def Foo2(): number 6851 def Foo2(): number
6728 return 40 6852 return 40
6749 " concrete type in the interface 6873 " concrete type in the interface
6750 def Test_implements_using_var_type_any() 6874 def Test_implements_using_var_type_any()
6751 var lines =<< trim END 6875 var lines =<< trim END
6752 vim9script 6876 vim9script
6753 interface A 6877 interface A
6754 this.val: list<dict<string>> 6878 var val: list<dict<string>>
6755 endinterface 6879 endinterface
6756 class B implements A 6880 class B implements A
6757 this.val = [{a: '1'}, {b: '2'}] 6881 var val = [{a: '1'}, {b: '2'}]
6758 endclass 6882 endclass
6759 var b = B.new() 6883 var b = B.new()
6760 assert_equal([{a: '1'}, {b: '2'}], b.val) 6884 assert_equal([{a: '1'}, {b: '2'}], b.val)
6761 END 6885 END
6762 v9.CheckSourceSuccess(lines) 6886 v9.CheckSourceSuccess(lines)
6763 6887
6764 # initialize instance variable using a different type 6888 # initialize instance variable using a different type
6765 lines =<< trim END 6889 lines =<< trim END
6766 vim9script 6890 vim9script
6767 interface A 6891 interface A
6768 this.val: list<dict<string>> 6892 var val: list<dict<string>>
6769 endinterface 6893 endinterface
6770 class B implements A 6894 class B implements A
6771 this.val = {a: 1, b: 2} 6895 var val = {a: 1, b: 2}
6772 endclass 6896 endclass
6773 var b = B.new() 6897 var b = B.new()
6774 END 6898 END
6775 v9.CheckSourceFailure(lines, 'E1382: Variable "val": type mismatch, expected list<dict<string>> but got dict<number>', 1) 6899 v9.CheckSourceFailure(lines, 'E1382: Variable "val": type mismatch, expected list<dict<string>> but got dict<number>', 1)
6776 enddef 6900 enddef
6779 def Test_nested_object_assignment() 6903 def Test_nested_object_assignment()
6780 var lines =<< trim END 6904 var lines =<< trim END
6781 vim9script 6905 vim9script
6782 6906
6783 class A 6907 class A
6784 this.value: number 6908 var value: number
6785 endclass 6909 endclass
6786 6910
6787 class B 6911 class B
6788 this.a: A = A.new() 6912 var a: A = A.new()
6789 endclass 6913 endclass
6790 6914
6791 class C 6915 class C
6792 this.b: B = B.new() 6916 var b: B = B.new()
6793 endclass 6917 endclass
6794 6918
6795 class D 6919 class D
6796 this.c: C = C.new() 6920 var c: C = C.new()
6797 endclass 6921 endclass
6798 6922
6799 def T(da: D) 6923 def T(da: D)
6800 da.c.b.a.value = 10 6924 da.c.b.a.value = 10
6801 enddef 6925 enddef
6891 def Test_dict_object_member() 7015 def Test_dict_object_member()
6892 var lines =<< trim END 7016 var lines =<< trim END
6893 vim9script 7017 vim9script
6894 7018
6895 class Context 7019 class Context
6896 public this.state: dict<number> = {} 7020 public var state: dict<number> = {}
6897 def GetState(): dict<number> 7021 def GetState(): dict<number>
6898 return this.state 7022 return this.state
6899 enddef 7023 enddef
6900 endclass 7024 endclass
6901 7025
6950 def Test_duplicate_variable() 7074 def Test_duplicate_variable()
6951 # Object variable name is same as the class variable name 7075 # Object variable name is same as the class variable name
6952 var lines =<< trim END 7076 var lines =<< trim END
6953 vim9script 7077 vim9script
6954 class A 7078 class A
6955 public static sval: number 7079 public static var sval: number
6956 public this.sval: number 7080 public var sval: number
6957 endclass 7081 endclass
6958 var a = A.new() 7082 var a = A.new()
6959 END 7083 END
6960 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: sval', 4) 7084 v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: sval', 4)
6961 7085
6962 # Duplicate variable name and calling a class method 7086 # Duplicate variable name and calling a class method
6963 lines =<< trim END 7087 lines =<< trim END
6964 vim9script 7088 vim9script
6965 class A 7089 class A
6966 public static sval: number 7090 public static var sval: number
6967 public this.sval: number 7091 public var sval: number
6968 def F1() 7092 def F1()
6969 echo this.sval 7093 echo this.sval
6970 enddef 7094 enddef
6971 static def F2() 7095 static def F2()
6972 echo sval 7096 echo sval
6978 7102
6979 # Duplicate variable with an empty constructor 7103 # Duplicate variable with an empty constructor
6980 lines =<< trim END 7104 lines =<< trim END
6981 vim9script 7105 vim9script
6982 class A 7106 class A
6983 public static sval: number 7107 public static var sval: number
6984 public this.sval: number 7108 public var sval: number
6985 def new() 7109 def new()
6986 enddef 7110 enddef
6987 endclass 7111 endclass
6988 var a = A.new() 7112 var a = A.new()
6989 END 7113 END
6997 'null_channel', 'null_job', 'super', 'this'] 7121 'null_channel', 'null_job', 'super', 'this']
6998 7122
6999 var lines =<< trim eval END 7123 var lines =<< trim eval END
7000 vim9script 7124 vim9script
7001 class C 7125 class C
7002 public this.{kword}: list<number> = [1, 2, 3] 7126 public var {kword}: list<number> = [1, 2, 3]
7003 endclass 7127 endclass
7004 var o = C.new() 7128 var o = C.new()
7005 END 7129 END
7006 v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3) 7130 v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3)
7007 7131
7008 lines =<< trim eval END 7132 lines =<< trim eval END
7009 vim9script 7133 vim9script
7010 class C 7134 class C
7011 public this.{kword}: list<number> = [1, 2, 3] 7135 public var {kword}: list<number> = [1, 2, 3]
7012 def new() 7136 def new()
7013 enddef 7137 enddef
7014 endclass 7138 endclass
7015 var o = C.new() 7139 var o = C.new()
7016 END 7140 END
7017 v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3) 7141 v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3)
7018 7142
7019 lines =<< trim eval END 7143 lines =<< trim eval END
7020 vim9script 7144 vim9script
7021 class C 7145 class C
7022 public this.{kword}: list<number> = [1, 2, 3] 7146 public var {kword}: list<number> = [1, 2, 3]
7023 def new() 7147 def new()
7024 enddef 7148 enddef
7025 def F() 7149 def F()
7026 echo this.{kword} 7150 echo this.{kword}
7027 enddef 7151 enddef
7034 # class variable name 7158 # class variable name
7035 if kword != 'this' 7159 if kword != 'this'
7036 lines =<< trim eval END 7160 lines =<< trim eval END
7037 vim9script 7161 vim9script
7038 class C 7162 class C
7039 public static {kword}: list<number> = [1, 2, 3] 7163 public static var {kword}: list<number> = [1, 2, 3]
7040 endclass 7164 endclass
7041 END 7165 END
7042 v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3) 7166 v9.CheckSourceFailure(lines, $'E1034: Cannot use reserved name {kword}', 3)
7043 endif 7167 endif
7044 endfor 7168 endfor
7167 vim9script 7291 vim9script
7168 def Foo(l: list<dict<blob>>): dict<list<blob>> 7292 def Foo(l: list<dict<blob>>): dict<list<blob>>
7169 return {} 7293 return {}
7170 enddef 7294 enddef
7171 class A 7295 class A
7172 public static Fn: func(list<dict<blob>>): dict<list<blob>> = Foo 7296 public static var Fn: func(list<dict<blob>>): dict<list<blob>> = Foo
7173 endclass 7297 endclass
7174 test_garbagecollect_now() 7298 test_garbagecollect_now()
7175 A.Fn = "abc" 7299 A.Fn = "abc"
7176 END 7300 END
7177 call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<blob>>): dict<list<blob>> but got string', 9) 7301 call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<blob>>): dict<list<blob>> but got string', 9)
7182 vim9script 7306 vim9script
7183 def Foo(l: list<dict<blob>>): dict<list<blob>> 7307 def Foo(l: list<dict<blob>>): dict<list<blob>>
7184 return {} 7308 return {}
7185 enddef 7309 enddef
7186 class A 7310 class A
7187 public static Fn: func(list<dict<blob>>): dict<list<blob>> = Foo 7311 public static var Fn: func(list<dict<blob>>): dict<list<blob>> = Foo
7188 def Bar() 7312 def Bar()
7189 Fn = "abc" 7313 Fn = "abc"
7190 enddef 7314 enddef
7191 endclass 7315 endclass
7192 var a = A.new() 7316 var a = A.new()
7201 vim9script 7325 vim9script
7202 def Foo(l: list<dict<blob>>): dict<list<blob>> 7326 def Foo(l: list<dict<blob>>): dict<list<blob>>
7203 return {} 7327 return {}
7204 enddef 7328 enddef
7205 class A 7329 class A
7206 public static Fn: func(list<dict<blob>>): dict<list<blob>> = Foo 7330 public static var Fn: func(list<dict<blob>>): dict<list<blob>> = Foo
7207 endclass 7331 endclass
7208 def Bar() 7332 def Bar()
7209 A.Fn = "abc" 7333 A.Fn = "abc"
7210 enddef 7334 enddef
7211 test_garbagecollect_now() 7335 test_garbagecollect_now()
7219 vim9script 7343 vim9script
7220 def Foo(l: list<dict<blob>>): dict<list<blob>> 7344 def Foo(l: list<dict<blob>>): dict<list<blob>>
7221 return {} 7345 return {}
7222 enddef 7346 enddef
7223 class A 7347 class A
7224 public static Fn = Foo 7348 public static var Fn = Foo
7225 endclass 7349 endclass
7226 test_garbagecollect_now() 7350 test_garbagecollect_now()
7227 A.Fn = "abc" 7351 A.Fn = "abc"
7228 END 7352 END
7229 call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<blob>>): dict<list<blob>> but got string', 9) 7353 call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<blob>>): dict<list<blob>> but got string', 9)
7234 vim9script 7358 vim9script
7235 def Foo(l: list<dict<blob>>): dict<list<blob>> 7359 def Foo(l: list<dict<blob>>): dict<list<blob>>
7236 return {} 7360 return {}
7237 enddef 7361 enddef
7238 class A 7362 class A
7239 public static Fn = Foo 7363 public static var Fn = Foo
7240 def Bar() 7364 def Bar()
7241 Fn = "abc" 7365 Fn = "abc"
7242 enddef 7366 enddef
7243 endclass 7367 endclass
7244 var a = A.new() 7368 var a = A.new()
7253 vim9script 7377 vim9script
7254 def Foo(l: list<dict<blob>>): dict<list<blob>> 7378 def Foo(l: list<dict<blob>>): dict<list<blob>>
7255 return {} 7379 return {}
7256 enddef 7380 enddef
7257 class A 7381 class A
7258 public static Fn = Foo 7382 public static var Fn = Foo
7259 endclass 7383 endclass
7260 def Bar() 7384 def Bar()
7261 A.Fn = "abc" 7385 A.Fn = "abc"
7262 enddef 7386 enddef
7263 test_garbagecollect_now() 7387 test_garbagecollect_now()
7270 vim9script 7394 vim9script
7271 def Foo(l: list<dict<blob>>): dict<list<blob>> 7395 def Foo(l: list<dict<blob>>): dict<list<blob>>
7272 return {} 7396 return {}
7273 enddef 7397 enddef
7274 class A 7398 class A
7275 public static Fn: any = Foo 7399 public static var Fn: any = Foo
7276 public static Fn2: any 7400 public static var Fn2: any
7277 endclass 7401 endclass
7278 test_garbagecollect_now() 7402 test_garbagecollect_now()
7279 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(A.Fn)) 7403 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(A.Fn))
7280 A.Fn = "abc" 7404 A.Fn = "abc"
7281 test_garbagecollect_now() 7405 test_garbagecollect_now()
7294 vim9script 7418 vim9script
7295 def Foo(l: list<dict<blob>>): dict<list<blob>> 7419 def Foo(l: list<dict<blob>>): dict<list<blob>>
7296 return {} 7420 return {}
7297 enddef 7421 enddef
7298 class A 7422 class A
7299 public static Fn: any = Foo 7423 public static var Fn: any = Foo
7300 public static Fn2: any 7424 public static var Fn2: any
7301 7425
7302 def Bar() 7426 def Bar()
7303 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(Fn)) 7427 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(Fn))
7304 Fn = "abc" 7428 Fn = "abc"
7305 assert_equal('string', typename(Fn)) 7429 assert_equal('string', typename(Fn))
7323 vim9script 7447 vim9script
7324 def Foo(l: list<dict<blob>>): dict<list<blob>> 7448 def Foo(l: list<dict<blob>>): dict<list<blob>>
7325 return {} 7449 return {}
7326 enddef 7450 enddef
7327 class A 7451 class A
7328 public static Fn: any = Foo 7452 public static var Fn: any = Foo
7329 public static Fn2: any 7453 public static var Fn2: any
7330 endclass 7454 endclass
7331 7455
7332 def Bar() 7456 def Bar()
7333 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(A.Fn)) 7457 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(A.Fn))
7334 A.Fn = "abc" 7458 A.Fn = "abc"
7346 call v9.CheckSourceSuccess(lines) 7470 call v9.CheckSourceSuccess(lines)
7347 7471
7348 let lines =<< trim END 7472 let lines =<< trim END
7349 vim9script 7473 vim9script
7350 class A 7474 class A
7351 public static foo = [0z10, 0z20] 7475 public static var foo = [0z10, 0z20]
7352 endclass 7476 endclass
7353 assert_equal([0z10, 0z20], A.foo) 7477 assert_equal([0z10, 0z20], A.foo)
7354 A.foo = [0z30] 7478 A.foo = [0z30]
7355 assert_equal([0z30], A.foo) 7479 assert_equal([0z30], A.foo)
7356 var a = A.foo 7480 var a = A.foo
7367 vim9script 7491 vim9script
7368 def Foo(l: list<dict<blob>>): dict<list<blob>> 7492 def Foo(l: list<dict<blob>>): dict<list<blob>>
7369 return {} 7493 return {}
7370 enddef 7494 enddef
7371 class A 7495 class A
7372 public this.Fn: func(list<dict<blob>>): dict<list<blob>> = Foo 7496 public var Fn: func(list<dict<blob>>): dict<list<blob>> = Foo
7373 endclass 7497 endclass
7374 var a = A.new() 7498 var a = A.new()
7375 test_garbagecollect_now() 7499 test_garbagecollect_now()
7376 a.Fn = "abc" 7500 a.Fn = "abc"
7377 END 7501 END
7383 vim9script 7507 vim9script
7384 def Foo(l: list<dict<blob>>): dict<list<blob>> 7508 def Foo(l: list<dict<blob>>): dict<list<blob>>
7385 return {} 7509 return {}
7386 enddef 7510 enddef
7387 class A 7511 class A
7388 public this.Fn: func(list<dict<blob>>): dict<list<blob>> = Foo 7512 public var Fn: func(list<dict<blob>>): dict<list<blob>> = Foo
7389 def Bar() 7513 def Bar()
7390 this.Fn = "abc" 7514 this.Fn = "abc"
7391 this.Fn = Foo 7515 this.Fn = Foo
7392 enddef 7516 enddef
7393 endclass 7517 endclass
7403 vim9script 7527 vim9script
7404 def Foo(l: list<dict<blob>>): dict<list<blob>> 7528 def Foo(l: list<dict<blob>>): dict<list<blob>>
7405 return {} 7529 return {}
7406 enddef 7530 enddef
7407 class A 7531 class A
7408 public this.Fn: func(list<dict<blob>>): dict<list<blob>> = Foo 7532 public var Fn: func(list<dict<blob>>): dict<list<blob>> = Foo
7409 endclass 7533 endclass
7410 def Bar() 7534 def Bar()
7411 var a = A.new() 7535 var a = A.new()
7412 a.Fn = "abc" 7536 a.Fn = "abc"
7413 a.Fn = Foo 7537 a.Fn = Foo
7423 vim9script 7547 vim9script
7424 def Foo(l: list<dict<blob>>): dict<list<blob>> 7548 def Foo(l: list<dict<blob>>): dict<list<blob>>
7425 return {} 7549 return {}
7426 enddef 7550 enddef
7427 class A 7551 class A
7428 public this.Fn = Foo 7552 public var Fn = Foo
7429 endclass 7553 endclass
7430 var a = A.new() 7554 var a = A.new()
7431 test_garbagecollect_now() 7555 test_garbagecollect_now()
7432 a.Fn = "abc" 7556 a.Fn = "abc"
7433 END 7557 END
7439 vim9script 7563 vim9script
7440 def Foo(l: list<dict<blob>>): dict<list<blob>> 7564 def Foo(l: list<dict<blob>>): dict<list<blob>>
7441 return {} 7565 return {}
7442 enddef 7566 enddef
7443 class A 7567 class A
7444 public this.Fn = Foo 7568 public var Fn = Foo
7445 def Bar() 7569 def Bar()
7446 this.Fn = "abc" 7570 this.Fn = "abc"
7447 this.Fn = Foo 7571 this.Fn = Foo
7448 enddef 7572 enddef
7449 endclass 7573 endclass
7459 vim9script 7583 vim9script
7460 def Foo(l: list<dict<blob>>): dict<list<blob>> 7584 def Foo(l: list<dict<blob>>): dict<list<blob>>
7461 return {} 7585 return {}
7462 enddef 7586 enddef
7463 class A 7587 class A
7464 public this.Fn = Foo 7588 public var Fn = Foo
7465 endclass 7589 endclass
7466 def Bar() 7590 def Bar()
7467 var a = A.new() 7591 var a = A.new()
7468 a.Fn = "abc" 7592 a.Fn = "abc"
7469 a.Fn = Foo 7593 a.Fn = Foo
7478 vim9script 7602 vim9script
7479 def Foo(l: list<dict<blob>>): dict<list<blob>> 7603 def Foo(l: list<dict<blob>>): dict<list<blob>>
7480 return {} 7604 return {}
7481 enddef 7605 enddef
7482 class A 7606 class A
7483 public this.Fn: any = Foo 7607 public var Fn: any = Foo
7484 public this.Fn2: any 7608 public var Fn2: any
7485 endclass 7609 endclass
7486 7610
7487 var a = A.new() 7611 var a = A.new()
7488 test_garbagecollect_now() 7612 test_garbagecollect_now()
7489 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(a.Fn)) 7613 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(a.Fn))
7504 vim9script 7628 vim9script
7505 def Foo(l: list<dict<blob>>): dict<list<blob>> 7629 def Foo(l: list<dict<blob>>): dict<list<blob>>
7506 return {} 7630 return {}
7507 enddef 7631 enddef
7508 class A 7632 class A
7509 public this.Fn: any = Foo 7633 public var Fn: any = Foo
7510 public this.Fn2: any 7634 public var Fn2: any
7511 7635
7512 def Bar() 7636 def Bar()
7513 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(this.Fn)) 7637 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(this.Fn))
7514 this.Fn = "abc" 7638 this.Fn = "abc"
7515 assert_equal('string', typename(this.Fn)) 7639 assert_equal('string', typename(this.Fn))
7534 vim9script 7658 vim9script
7535 def Foo(l: list<dict<blob>>): dict<list<blob>> 7659 def Foo(l: list<dict<blob>>): dict<list<blob>>
7536 return {} 7660 return {}
7537 enddef 7661 enddef
7538 class A 7662 class A
7539 public this.Fn: any = Foo 7663 public var Fn: any = Foo
7540 public this.Fn2: any 7664 public var Fn2: any
7541 endclass 7665 endclass
7542 7666
7543 def Bar() 7667 def Bar()
7544 var a = A.new() 7668 var a = A.new()
7545 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(a.Fn)) 7669 assert_equal('func(list<dict<blob>>): dict<list<blob>>', typename(a.Fn))
7562 " use-after-free error. 7686 " use-after-free error.
7563 def Test_recursive_object_method_call() 7687 def Test_recursive_object_method_call()
7564 var lines =<< trim END 7688 var lines =<< trim END
7565 vim9script 7689 vim9script
7566 class A 7690 class A
7567 this.val: number = 0 7691 var val: number = 0
7568 def Foo(): number 7692 def Foo(): number
7569 if this.val >= 90 7693 if this.val >= 90
7570 return this.val 7694 return this.val
7571 endif 7695 endif
7572 this.val += 1 7696 this.val += 1
7582 " Test for recursively calling a class method. 7706 " Test for recursively calling a class method.
7583 def Test_recursive_class_method_call() 7707 def Test_recursive_class_method_call()
7584 var lines =<< trim END 7708 var lines =<< trim END
7585 vim9script 7709 vim9script
7586 class A 7710 class A
7587 static val: number = 0 7711 static var val: number = 0
7588 static def Foo(): number 7712 static def Foo(): number
7589 if val >= 90 7713 if val >= 90
7590 return val 7714 return val
7591 endif 7715 endif
7592 val += 1 7716 val += 1
7687 def Test_op_and_assignment() 7811 def Test_op_and_assignment()
7688 # Using += with a class variable 7812 # Using += with a class variable
7689 var lines =<< trim END 7813 var lines =<< trim END
7690 vim9script 7814 vim9script
7691 class A 7815 class A
7692 public static val: list<number> = [] 7816 public static var val: list<number> = []
7693 static def Foo(): list<number> 7817 static def Foo(): list<number>
7694 val += [1] 7818 val += [1]
7695 return val 7819 return val
7696 enddef 7820 enddef
7697 endclass 7821 endclass
7708 7832
7709 # Using += with an object variable 7833 # Using += with an object variable
7710 lines =<< trim END 7834 lines =<< trim END
7711 vim9script 7835 vim9script
7712 class A 7836 class A
7713 public this.val: list<number> = [] 7837 public var val: list<number> = []
7714 def Foo(): list<number> 7838 def Foo(): list<number>
7715 this.val += [1] 7839 this.val += [1]
7716 return this.val 7840 return this.val
7717 enddef 7841 enddef
7718 endclass 7842 endclass
7764 7888
7765 # Using object method funcref at the script level 7889 # Using object method funcref at the script level
7766 lines =<< trim END 7890 lines =<< trim END
7767 vim9script 7891 vim9script
7768 class A 7892 class A
7769 this.val: number 7893 var val: number
7770 def Foo(): number 7894 def Foo(): number
7771 return this.val 7895 return this.val
7772 enddef 7896 enddef
7773 endclass 7897 endclass
7774 var a = A.new(345) 7898 var a = A.new(345)
7874 7998
7875 # Using object method funcref using call() 7999 # Using object method funcref using call()
7876 lines =<< trim END 8000 lines =<< trim END
7877 vim9script 8001 vim9script
7878 class A 8002 class A
7879 this.val: number 8003 var val: number
7880 def Foo(): number 8004 def Foo(): number
7881 return this.val 8005 return this.val
7882 enddef 8006 enddef
7883 endclass 8007 endclass
7884 8008
7926 8050
7927 # Using class method funcref at the script level 8051 # Using class method funcref at the script level
7928 lines =<< trim END 8052 lines =<< trim END
7929 vim9script 8053 vim9script
7930 class A 8054 class A
7931 public static val: number 8055 public static var val: number
7932 static def Foo(): number 8056 static def Foo(): number
7933 return val 8057 return val
7934 enddef 8058 enddef
7935 endclass 8059 endclass
7936 A.val = 567 8060 A.val = 567
8030 8154
8031 # Using class method funcref using call() 8155 # Using class method funcref using call()
8032 lines =<< trim END 8156 lines =<< trim END
8033 vim9script 8157 vim9script
8034 class A 8158 class A
8035 public static val: number 8159 public static var val: number
8036 static def Foo(): number 8160 static def Foo(): number
8037 return val 8161 return val
8038 enddef 8162 enddef
8039 endclass 8163 endclass
8040 8164
8056 def Foo(n: number): number 8180 def Foo(n: number): number
8057 return n * 10 8181 return n * 10
8058 enddef 8182 enddef
8059 8183
8060 class A 8184 class A
8061 this.Cb: func(number): number = Foo 8185 var Cb: func(number): number = Foo
8062 def Bar() 8186 def Bar()
8063 assert_equal(200, this.Cb(20)) 8187 assert_equal(200, this.Cb(20))
8064 enddef 8188 enddef
8065 endclass 8189 endclass
8066 8190
8075 def Foo(n: number): number 8199 def Foo(n: number): number
8076 return n * 10 8200 return n * 10
8077 enddef 8201 enddef
8078 8202
8079 class A 8203 class A
8080 this.Cb: func(number): number = Foo 8204 var Cb: func(number): number = Foo
8081 endclass 8205 endclass
8082 8206
8083 def Bar() 8207 def Bar()
8084 var a = A.new() 8208 var a = A.new()
8085 assert_equal(200, a.Cb(20)) 8209 assert_equal(200, a.Cb(20))
8094 def Foo(n: number): number 8218 def Foo(n: number): number
8095 return n * 10 8219 return n * 10
8096 enddef 8220 enddef
8097 8221
8098 class A 8222 class A
8099 this.Cb: func(number): number = Foo 8223 var Cb: func(number): number = Foo
8100 endclass 8224 endclass
8101 8225
8102 var a = A.new() 8226 var a = A.new()
8103 assert_equal(200, a.Cb(20)) 8227 assert_equal(200, a.Cb(20))
8104 END 8228 END
8107 # Using a funcref object variable pointing to an object method in an object 8231 # Using a funcref object variable pointing to an object method in an object
8108 # method. 8232 # method.
8109 lines =<< trim END 8233 lines =<< trim END
8110 vim9script 8234 vim9script
8111 class A 8235 class A
8112 this.Cb: func(number): number = this.Foo 8236 var Cb: func(number): number = this.Foo
8113 def Foo(n: number): number 8237 def Foo(n: number): number
8114 return n * 10 8238 return n * 10
8115 enddef 8239 enddef
8116 def Bar() 8240 def Bar()
8117 assert_equal(200, this.Cb(20)) 8241 assert_equal(200, this.Cb(20))
8126 # Using a funcref object variable pointing to an object method in a def 8250 # Using a funcref object variable pointing to an object method in a def
8127 # method. 8251 # method.
8128 lines =<< trim END 8252 lines =<< trim END
8129 vim9script 8253 vim9script
8130 class A 8254 class A
8131 this.Cb: func(number): number = this.Foo 8255 var Cb: func(number): number = this.Foo
8132 def Foo(n: number): number 8256 def Foo(n: number): number
8133 return n * 10 8257 return n * 10
8134 enddef 8258 enddef
8135 endclass 8259 endclass
8136 8260
8145 # Using a funcref object variable pointing to an object method at script 8269 # Using a funcref object variable pointing to an object method at script
8146 # level. 8270 # level.
8147 lines =<< trim END 8271 lines =<< trim END
8148 vim9script 8272 vim9script
8149 class A 8273 class A
8150 this.Cb = this.Foo 8274 var Cb = this.Foo
8151 def Foo(n: number): number 8275 def Foo(n: number): number
8152 return n * 10 8276 return n * 10
8153 enddef 8277 enddef
8154 endclass 8278 endclass
8155 8279
8167 def Foo(n: number): number 8291 def Foo(n: number): number
8168 return n * 10 8292 return n * 10
8169 enddef 8293 enddef
8170 8294
8171 class A 8295 class A
8172 static Cb = Foo 8296 static var Cb = Foo
8173 static def Bar() 8297 static def Bar()
8174 assert_equal(200, Cb(20)) 8298 assert_equal(200, Cb(20))
8175 enddef 8299 enddef
8176 endclass 8300 endclass
8177 8301
8185 def Foo(n: number): number 8309 def Foo(n: number): number
8186 return n * 10 8310 return n * 10
8187 enddef 8311 enddef
8188 8312
8189 class A 8313 class A
8190 public static Cb = Foo 8314 public static var Cb = Foo
8191 endclass 8315 endclass
8192 8316
8193 def Bar() 8317 def Bar()
8194 assert_equal(200, A.Cb(20)) 8318 assert_equal(200, A.Cb(20))
8195 enddef 8319 enddef
8203 def Foo(n: number): number 8327 def Foo(n: number): number
8204 return n * 10 8328 return n * 10
8205 enddef 8329 enddef
8206 8330
8207 class A 8331 class A
8208 public static Cb = Foo 8332 public static var Cb = Foo
8209 endclass 8333 endclass
8210 8334
8211 assert_equal(200, A.Cb(20)) 8335 assert_equal(200, A.Cb(20))
8212 END 8336 END
8213 v9.CheckSourceSuccess(lines) 8337 v9.CheckSourceSuccess(lines)
8215 # Using a funcref class variable pointing to a class method in a class 8339 # Using a funcref class variable pointing to a class method in a class
8216 # method. 8340 # method.
8217 lines =<< trim END 8341 lines =<< trim END
8218 vim9script 8342 vim9script
8219 class A 8343 class A
8220 static Cb: func(number): number 8344 static var Cb: func(number): number
8221 static def Foo(n: number): number 8345 static def Foo(n: number): number
8222 return n * 10 8346 return n * 10
8223 enddef 8347 enddef
8224 static def Init() 8348 static def Init()
8225 Cb = Foo 8349 Cb = Foo
8236 8360
8237 # Using a funcref class variable pointing to a class method in a def method. 8361 # Using a funcref class variable pointing to a class method in a def method.
8238 lines =<< trim END 8362 lines =<< trim END
8239 vim9script 8363 vim9script
8240 class A 8364 class A
8241 static Cb: func(number): number 8365 static var Cb: func(number): number
8242 static def Foo(n: number): number 8366 static def Foo(n: number): number
8243 return n * 10 8367 return n * 10
8244 enddef 8368 enddef
8245 static def Init() 8369 static def Init()
8246 Cb = Foo 8370 Cb = Foo
8257 8381
8258 # Using a funcref class variable pointing to a class method at script level. 8382 # Using a funcref class variable pointing to a class method at script level.
8259 lines =<< trim END 8383 lines =<< trim END
8260 vim9script 8384 vim9script
8261 class A 8385 class A
8262 static Cb: func(number): number 8386 static var Cb: func(number): number
8263 static def Foo(n: number): number 8387 static def Foo(n: number): number
8264 return n * 10 8388 return n * 10
8265 enddef 8389 enddef
8266 static def Init() 8390 static def Init()
8267 Cb = Foo 8391 Cb = Foo
8279 # Use the popup from the script level 8403 # Use the popup from the script level
8280 var lines =<< trim END 8404 var lines =<< trim END
8281 vim9script 8405 vim9script
8282 8406
8283 class A 8407 class A
8284 this.selection: number = -1 8408 var selection: number = -1
8285 this.filterkeys: list<string> = [] 8409 var filterkeys: list<string> = []
8286 8410
8287 def PopupFilter(id: number, key: string): bool 8411 def PopupFilter(id: number, key: string): bool
8288 add(this.filterkeys, key) 8412 add(this.filterkeys, key)
8289 return popup_filter_yesno(id, key) 8413 return popup_filter_yesno(id, key)
8290 enddef 8414 enddef
8315 # Use the popup from a def function 8439 # Use the popup from a def function
8316 lines =<< trim END 8440 lines =<< trim END
8317 vim9script 8441 vim9script
8318 8442
8319 class A 8443 class A
8320 this.selection: number = -1 8444 var selection: number = -1
8321 this.filterkeys: list<string> = [] 8445 var filterkeys: list<string> = []
8322 8446
8323 def PopupFilter(id: number, key: string): bool 8447 def PopupFilter(id: number, key: string): bool
8324 add(this.filterkeys, key) 8448 add(this.filterkeys, key)
8325 return popup_filter_yesno(id, key) 8449 return popup_filter_yesno(id, key)
8326 enddef 8450 enddef
8357 # Use the popup from the script level 8481 # Use the popup from the script level
8358 var lines =<< trim END 8482 var lines =<< trim END
8359 vim9script 8483 vim9script
8360 8484
8361 class A 8485 class A
8362 static selection: number = -1 8486 static var selection: number = -1
8363 static filterkeys: list<string> = [] 8487 static var filterkeys: list<string> = []
8364 8488
8365 static def PopupFilter(id: number, key: string): bool 8489 static def PopupFilter(id: number, key: string): bool
8366 add(filterkeys, key) 8490 add(filterkeys, key)
8367 return popup_filter_yesno(id, key) 8491 return popup_filter_yesno(id, key)
8368 enddef 8492 enddef
8392 # Use the popup from a def function 8516 # Use the popup from a def function
8393 lines =<< trim END 8517 lines =<< trim END
8394 vim9script 8518 vim9script
8395 8519
8396 class A 8520 class A
8397 static selection: number = -1 8521 static var selection: number = -1
8398 static filterkeys: list<string> = [] 8522 static var filterkeys: list<string> = []
8399 8523
8400 static def PopupFilter(id: number, key: string): bool 8524 static def PopupFilter(id: number, key: string): bool
8401 add(filterkeys, key) 8525 add(filterkeys, key)
8402 return popup_filter_yesno(id, key) 8526 return popup_filter_yesno(id, key)
8403 enddef 8527 enddef
8433 # Use the timer callback from script level 8557 # Use the timer callback from script level
8434 var lines =<< trim END 8558 var lines =<< trim END
8435 vim9script 8559 vim9script
8436 8560
8437 class A 8561 class A
8438 this.timerTick: number = -1 8562 var timerTick: number = -1
8439 def TimerCb(timerID: number) 8563 def TimerCb(timerID: number)
8440 this.timerTick = 6 8564 this.timerTick = 6
8441 enddef 8565 enddef
8442 endclass 8566 endclass
8443 8567
8455 # Use the timer callback from a def function 8579 # Use the timer callback from a def function
8456 lines =<< trim END 8580 lines =<< trim END
8457 vim9script 8581 vim9script
8458 8582
8459 class A 8583 class A
8460 this.timerTick: number = -1 8584 var timerTick: number = -1
8461 def TimerCb(timerID: number) 8585 def TimerCb(timerID: number)
8462 this.timerTick = 6 8586 this.timerTick = 6
8463 enddef 8587 enddef
8464 endclass 8588 endclass
8465 8589
8483 # Use the timer callback from script level 8607 # Use the timer callback from script level
8484 var lines =<< trim END 8608 var lines =<< trim END
8485 vim9script 8609 vim9script
8486 8610
8487 class A 8611 class A
8488 static timerTick: number = -1 8612 static var timerTick: number = -1
8489 static def TimerCb(timerID: number) 8613 static def TimerCb(timerID: number)
8490 timerTick = 6 8614 timerTick = 6
8491 enddef 8615 enddef
8492 endclass 8616 endclass
8493 8617
8504 # Use the timer callback from a def function 8628 # Use the timer callback from a def function
8505 lines =<< trim END 8629 lines =<< trim END
8506 vim9script 8630 vim9script
8507 8631
8508 class A 8632 class A
8509 static timerTick: number = -1 8633 static var timerTick: number = -1
8510 static def TimerCb(timerID: number) 8634 static def TimerCb(timerID: number)
8511 timerTick = 6 8635 timerTick = 6
8512 enddef 8636 enddef
8513 endclass 8637 endclass
8514 8638
8530 " operator. 8654 " operator.
8531 def Test_class_variable_as_operands() 8655 def Test_class_variable_as_operands()
8532 var lines =<< trim END 8656 var lines =<< trim END
8533 vim9script 8657 vim9script
8534 class Tests 8658 class Tests
8535 static truthy: bool = true 8659 static var truthy: bool = true
8536 public static TruthyFn: func 8660 public static var TruthyFn: func
8537 static list: list<any> = [] 8661 static var list: list<any> = []
8538 static four: number = 4 8662 static var four: number = 4
8539 static str: string = 'hello' 8663 static var str: string = 'hello'
8540 8664
8541 static def Str(): string 8665 static def Str(): string
8542 return str 8666 return str
8543 enddef 8667 enddef
8544 8668
8635 def Test_dict_member_key_type_check() 8759 def Test_dict_member_key_type_check()
8636 var lines =<< trim END 8760 var lines =<< trim END
8637 vim9script 8761 vim9script
8638 8762
8639 abstract class State 8763 abstract class State
8640 this.numbers: dict<string> = {0: 'nil', 1: 'unity'} 8764 var numbers: dict<string> = {0: 'nil', 1: 'unity'}
8641 endclass 8765 endclass
8642 8766
8643 class Test extends State 8767 class Test extends State
8644 def ObjMethodTests() 8768 def ObjMethodTests()
8645 var cursor: number = 0 8769 var cursor: number = 0
8720 8844
8721 lines =<< trim END 8845 lines =<< trim END
8722 vim9script 8846 vim9script
8723 8847
8724 class A 8848 class A
8725 this.numbers: dict<string> = {a: '1', b: '2'} 8849 var numbers: dict<string> = {a: '1', b: '2'}
8726 8850
8727 def new() 8851 def new()
8728 enddef 8852 enddef
8729 8853
8730 def Foo() 8854 def Foo()
8740 8864
8741 lines =<< trim END 8865 lines =<< trim END
8742 vim9script 8866 vim9script
8743 8867
8744 class A 8868 class A
8745 this.numbers: dict<number> = {a: 1, b: 2} 8869 var numbers: dict<number> = {a: 1, b: 2}
8746 8870
8747 def new() 8871 def new()
8748 enddef 8872 enddef
8749 8873
8750 def Foo() 8874 def Foo()