Mercurial > vim
comparison src/testdir/test_vim9_class.vim @ 32960:d5c05e15cf81 v9.0.1780
patch 9.0.1780: Vim9 type not defined during object creation
Commit: https://github.com/vim/vim/commit/618e47d1cd93954bad26d47e5353b4f1432daa5e
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Tue Aug 22 21:29:28 2023 +0200
patch 9.0.1780: Vim9 type not defined during object creation
Problem: Vim9 type not defined during object creation
Solution: Define type during object creation and not during class
definition, parse mulit-line member initializers, fix lock
initialization
If type is not specified for a member, set it during object creation
instead of during class definition. Add a runtime type check for the
object member initialization expression
Also, while at it, when copying an object or class, make sure the lock
is correctly initialized.
And finally, parse multi-line member initializers correctly.
closes: #11957
closes: #12868
closes: #12869
closes: #12881
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Co-authored-by: LemonBoy <thatlemon@gmail.com>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 22 Aug 2023 21:45:02 +0200 |
parents | 54c01bb98b8e |
children | e4851934751a |
comparison
equal
deleted
inserted
replaced
32959:a0266fbc89f5 | 32960:d5c05e15cf81 |
---|---|
208 endclass | 208 endclass |
209 var a = A.new() | 209 var a = A.new() |
210 var v = a.Foo(,) | 210 var v = a.Foo(,) |
211 END | 211 END |
212 v9.CheckScriptFailure(lines, 'E15:') | 212 v9.CheckScriptFailure(lines, 'E15:') |
213 | |
214 lines =<< trim END | |
215 vim9script | |
216 class A | |
217 this.y = { | |
218 X: 1 | |
219 } | |
220 endclass | |
221 var a = A.new() | |
222 END | |
223 v9.CheckScriptSuccess(lines) | |
213 enddef | 224 enddef |
214 | 225 |
215 def Test_class_defined_twice() | 226 def Test_class_defined_twice() |
216 # class defined twice should fail | 227 # class defined twice should fail |
217 var lines =<< trim END | 228 var lines =<< trim END |
666 this.col = 1 | 677 this.col = 1 |
667 endclass | 678 endclass |
668 END | 679 END |
669 v9.CheckScriptFailure(lines, 'E1022:') | 680 v9.CheckScriptFailure(lines, 'E1022:') |
670 | 681 |
671 lines =<< trim END | 682 # If the type is not specified for a member, then it should be set during |
672 vim9script | 683 # object creation and not when defining the class. |
673 class TextPosition | 684 lines =<< trim END |
674 this.lnum = v:none | 685 vim9script |
686 | |
687 var init_count = 0 | |
688 def Init(): string | |
689 init_count += 1 | |
690 return 'foo' | |
691 enddef | |
692 | |
693 class A | |
694 this.str1 = Init() | |
695 this.str2: string = Init() | |
675 this.col = 1 | 696 this.col = 1 |
676 endclass | 697 endclass |
677 END | 698 |
678 v9.CheckScriptFailure(lines, 'E1330:') | 699 assert_equal(init_count, 0) |
700 var a = A.new() | |
701 assert_equal(init_count, 2) | |
702 END | |
703 v9.CheckScriptSuccess(lines) | |
679 | 704 |
680 # Test for initializing an object member with an unknown variable/type | 705 # Test for initializing an object member with an unknown variable/type |
681 lines =<< trim END | 706 lines =<< trim END |
682 vim9script | 707 vim9script |
683 class A | 708 class A |
684 this.value = init_val | 709 this.value = init_val |
685 endclass | 710 endclass |
686 END | 711 var a = A.new() |
687 v9.CheckScriptFailureList(lines, ['E121:', 'E1329:']) | 712 END |
713 v9.CheckScriptFailure(lines, 'E1001:') | |
688 enddef | 714 enddef |
689 | 715 |
690 def Test_class_object_member_access() | 716 def Test_class_object_member_access() |
691 var lines =<< trim END | 717 var lines =<< trim END |
692 vim9script | 718 vim9script |
2623 assert_equal('object<C>', typename(c)) | 2649 assert_equal('object<C>', typename(c)) |
2624 END | 2650 END |
2625 v9.CheckScriptFailure(lines, 'E1365:') | 2651 v9.CheckScriptFailure(lines, 'E1365:') |
2626 enddef | 2652 enddef |
2627 | 2653 |
2654 " Test for checking a member initialization type at run time. | |
2655 def Test_runtime_type_check_for_member_init() | |
2656 var lines =<< trim END | |
2657 vim9script | |
2658 | |
2659 var retnum: bool = false | |
2660 | |
2661 def F(): any | |
2662 retnum = !retnum | |
2663 if retnum | |
2664 return 1 | |
2665 else | |
2666 return "hello" | |
2667 endif | |
2668 enddef | |
2669 | |
2670 class C | |
2671 this._foo: bool = F() | |
2672 endclass | |
2673 | |
2674 var c1 = C.new() | |
2675 var c2 = C.new() | |
2676 END | |
2677 v9.CheckScriptFailure(lines, 'E1012:') | |
2678 enddef | |
2679 | |
2680 " Test for locking a variable referring to an object and reassigning to another | |
2681 " object. | |
2682 def Test_object_lockvar() | |
2683 var lines =<< trim END | |
2684 vim9script | |
2685 | |
2686 class C | |
2687 this.val: number | |
2688 def new(this.val) | |
2689 enddef | |
2690 endclass | |
2691 | |
2692 var some_dict: dict<C> = { a: C.new(1), b: C.new(2), c: C.new(3), } | |
2693 lockvar 2 some_dict | |
2694 | |
2695 var current: C | |
2696 current = some_dict['c'] | |
2697 assert_equal(3, current.val) | |
2698 current = some_dict['b'] | |
2699 assert_equal(2, current.val) | |
2700 | |
2701 def F() | |
2702 current = some_dict['c'] | |
2703 enddef | |
2704 | |
2705 def G() | |
2706 current = some_dict['b'] | |
2707 enddef | |
2708 | |
2709 F() | |
2710 assert_equal(3, current.val) | |
2711 G() | |
2712 assert_equal(2, current.val) | |
2713 END | |
2714 v9.CheckScriptSuccess(lines) | |
2715 enddef | |
2716 | |
2628 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker | 2717 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |