0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 9.1.1264: Vim9: error when comparing objects

Problem:  Vim9: error when comparing objects
          (lifepillar)
Solution: When comparing object types, compare their classes
          (Yegappan Lakshmanan)

fixes: #17014
closes: #17018

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Yegappan Lakshmanan 2025-03-31 20:35:44 +02:00 committed by Christian Brabandt
parent 20393bc02d
commit b1d6db0be5
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
3 changed files with 46 additions and 2 deletions

View File

@ -12481,4 +12481,44 @@ def Test_super_keyword()
v9.CheckSourceSuccess(lines)
enddef
" Test for using a list of objects
def Test_method_call_from_list_of_objects()
var lines =<< trim END
vim9script
class C
def F(): string
return 'C.F'
enddef
endclass
class D
var x: string
def new(this.x)
enddef
def F(): string
return 'D.F'
enddef
endclass
var obj1 = C.new()
var obj2 = D.new('a')
def CheckObjectList()
var items = [obj1, obj2]
assert_equal('list<any>', typename(items))
assert_equal('C.F', items[0].F())
assert_equal('D.F', items[1].F())
enddef
CheckObjectList()
var items2 = [obj1, obj2]
assert_equal('list<any>', typename(items2))
assert_equal('C.F', items2[0].F())
assert_equal('D.F', items2[1].F())
END
v9.CheckSourceSuccess(lines)
enddef
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

View File

@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1264,
/**/
1263,
/**/

View File

@ -1984,10 +1984,12 @@ equal_type(type_T *type1, type_T *type2, int flags)
case VAR_JOB:
case VAR_CHANNEL:
case VAR_INSTR:
case VAR_CLASS:
case VAR_OBJECT:
case VAR_TYPEALIAS:
break; // not composite is always OK
case VAR_OBJECT:
case VAR_CLASS:
// Objects are considered equal if they are from the same class
return type1->tt_class == type2->tt_class;
case VAR_LIST:
case VAR_DICT:
return equal_type(type1->tt_member, type2->tt_member, flags);