0
0
mirror of https://github.com/vim/vim.git synced 2025-10-05 05:34:07 -04:00

patch 9.0.1760: vim9 class problem with new() constructor

Problem:  vim9 class problem with new() constructor
Solution: Don't allow a return type for the new() class constructor.

closes: #12863
closes: #12040

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
Yegappan Lakshmanan
2023-08-20 18:20:17 +02:00
committed by Christian Brabandt
parent 6cc8bc8366
commit 6ac1544e13
6 changed files with 152 additions and 8 deletions

View File

@@ -1577,6 +1577,16 @@ def Test_class_implements_interface()
END
v9.CheckScriptFailure(lines, 'E1349:')
# implements should be followed by a white space
lines =<< trim END
vim9script
interface A
endinterface
class B implements A;
endclass
END
v9.CheckScriptFailure(lines, 'E1315:')
lines =<< trim END
vim9script
@@ -2515,4 +2525,104 @@ def Test_stack_expansion_with_methods()
END
v9.CheckScriptSuccess(lines)
enddef
" Test the return type of the new() constructor
def Test_new_return_type()
# new() uses the default return type and there is no return statement
var lines =<< trim END
vim9script
class C
this._bufnr: number
def new(this._bufnr)
if !bufexists(this._bufnr)
this._bufnr = -1
endif
enddef
endclass
var c = C.new(12345)
assert_equal('object<C>', typename(c))
var v1: C
v1 = C.new(12345)
assert_equal('object<C>', typename(v1))
def F()
var v2: C
v2 = C.new(12345)
assert_equal('object<C>', typename(v2))
enddef
F()
END
v9.CheckScriptSuccess(lines)
# new() uses the default return type and an empty 'return' statement
lines =<< trim END
vim9script
class C
this._bufnr: number
def new(this._bufnr)
if !bufexists(this._bufnr)
this._bufnr = -1
return
endif
enddef
endclass
var c = C.new(12345)
assert_equal('object<C>', typename(c))
var v1: C
v1 = C.new(12345)
assert_equal('object<C>', typename(v1))
def F()
var v2: C
v2 = C.new(12345)
assert_equal('object<C>', typename(v2))
enddef
F()
END
v9.CheckScriptSuccess(lines)
# new() uses "any" return type and returns "this"
lines =<< trim END
vim9script
class C
this._bufnr: number
def new(this._bufnr): any
if !bufexists(this._bufnr)
this._bufnr = -1
return this
endif
enddef
endclass
END
v9.CheckScriptFailure(lines, 'E1365:')
# new() uses 'Dict' return type and returns a Dict
lines =<< trim END
vim9script
class C
this._state: dict<any>
def new(): dict<any>
this._state = {}
return this._state
enddef
endclass
var c = C.new()
assert_equal('object<C>', typename(c))
END
v9.CheckScriptFailure(lines, 'E1365:')
enddef
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker