Mercurial > vim
comparison src/testdir/test_vim9_builtin.vim @ 27517:f00a7a2bee21 v8.2.4286
patch 8.2.4286: Vim9: strict type checking after copy() and deepcopy()
Commit: https://github.com/vim/vim/commit/381692b6f1c2ec9b73a139500286ddc9347a1c01
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Feb 2 20:01:27 2022 +0000
patch 8.2.4286: Vim9: strict type checking after copy() and deepcopy()
Problem: Vim9: strict type checking after copy() and deepcopy().
Solution: Allow type to change after making a copy. (closes https://github.com/vim/vim/issues/9644)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 02 Feb 2022 21:15:03 +0100 |
parents | 4cea92e99a5a |
children | 2397b18d6f94 |
comparison
equal
deleted
inserted
replaced
27516:fdcbdd530e4c | 27517:f00a7a2bee21 |
---|---|
718 res += n | 718 res += n |
719 endfor | 719 endfor |
720 res->assert_equal(6) | 720 res->assert_equal(6) |
721 | 721 |
722 dl = deepcopy([1, 2, 3], true) | 722 dl = deepcopy([1, 2, 3], true) |
723 | |
724 # after a copy() the type can change, but not the item itself | |
725 var nl: list<number> = [1, 2] | |
726 assert_equal([1, 2, 'x'], nl->copy()->extend(['x'])) | |
727 | |
728 var lines =<< trim END | |
729 var nll: list<list<number>> = [[1, 2]] | |
730 nll->copy()[0]->extend(['x']) | |
731 END | |
732 v9.CheckDefExecAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected list<number> but got list<string> in extend()') | |
733 | |
734 var nd: dict<number> = {a: 1, b: 2} | |
735 assert_equal({a: 1, b: 2, c: 'x'}, nd->copy()->extend({c: 'x'})) | |
736 lines =<< trim END | |
737 var ndd: dict<dict<number>> = {a: {x: 1, y: 2}} | |
738 ndd->copy()['a']->extend({z: 'x'}) | |
739 END | |
740 v9.CheckDefExecAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string> in extend()') | |
741 | |
742 # after a deepcopy() the item type can also change | |
743 var nll: list<list<number>> = [[1, 2]] | |
744 assert_equal([1, 2, 'x'], nll->deepcopy()[0]->extend(['x'])) | |
745 | |
746 var ndd: dict<dict<number>> = {a: {x: 1, y: 2}} | |
747 assert_equal({x: 1, y: 2, z: 'x'}, ndd->deepcopy()['a']->extend({z: 'x'})) | |
723 enddef | 748 enddef |
724 | 749 |
725 def Test_count() | 750 def Test_count() |
726 count('ABC ABC ABC', 'b', true)->assert_equal(3) | 751 count('ABC ABC ABC', 'b', true)->assert_equal(3) |
727 count('ABC ABC ABC', 'b', false)->assert_equal(0) | 752 count('ABC ABC ABC', 'b', false)->assert_equal(0) |