Skip to content

Commit 7e89800

Browse files
yegappanchrisbra
authored andcommitted
patch 9.1.1105: Vim9: no support for protected new() method
Problem: Vim9: no support for protected new() method Solution: support the protected "_new()" object method (Yegappan Lakshmanan) closes: #16604 Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent f0ed0e6 commit 7e89800

5 files changed

Lines changed: 48 additions & 4 deletions

File tree

runtime/doc/version9.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*version9.txt* For Vim version 9.1. Last change: 2025 Feb 08
1+
*version9.txt* For Vim version 9.1. Last change: 2025 Feb 11
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -41551,6 +41551,8 @@ Add support for internal builtin functions with vim9 objects, see
4155141551

4155241552
Enum support for Vim9 script |:enum|
4155341553

41554+
Support for protected _new() method
41555+
4155441556
*new-other-9.2*
4155541557
Other new features ~
4155641558
------------------

runtime/doc/vim9class.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*vim9class.txt* For Vim version 9.1. Last change: 2024 Dec 29
1+
*vim9class.txt* For Vim version 9.1. Last change: 2025 Feb 11
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -272,6 +272,9 @@ no need to call "super()" or "new()" on the parent.
272272
When defining the new() method the return type should not be specified. It
273273
always returns an object of the class.
274274

275+
The new() method can be made a protected method by using "_new()". This can
276+
be used to support the singleton design pattern.
277+
275278
*E1386*
276279
When invoking an object method, the method name should be preceded by the
277280
object variable name. An object method cannot be invoked using the class

src/testdir/test_vim9_class.vim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12374,4 +12374,39 @@ def Test_abstract_method_across_hierarchy()
1237412374
v9.CheckSourceSuccess(lines)
1237512375
enddef
1237612376

12377+
" Test for using a protected new() method (singleton design pattern)
12378+
def Test_protected_new_method()
12379+
var lines =<< trim END
12380+
vim9script
12381+
class A
12382+
def _new()
12383+
enddef
12384+
endclass
12385+
var a = A.new()
12386+
END
12387+
v9.CheckSourceFailure(lines, 'E1325: Method "new" not found in class "A"', 6)
12388+
12389+
lines =<< trim END
12390+
vim9script
12391+
class A
12392+
static var _instance: A
12393+
var str: string
12394+
def _new(str: string)
12395+
this.str = str
12396+
enddef
12397+
static def GetInstance(str: string): A
12398+
if _instance == null
12399+
_instance = A._new(str)
12400+
endif
12401+
return _instance
12402+
enddef
12403+
endclass
12404+
var a: A = A.GetInstance('foo')
12405+
var b: A = A.GetInstance('bar')
12406+
assert_equal('foo', a.str)
12407+
assert_equal('foo', b.str)
12408+
END
12409+
v9.CheckSourceSuccess(lines)
12410+
enddef
12411+
1237712412
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1105,
707709
/**/
708710
1104,
709711
/**/

src/vim9class.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,8 @@ ex_class(exarg_T *eap)
23752375
{
23762376
exarg_T ea;
23772377
garray_T lines_to_free;
2378-
int is_new = STRNCMP(p, "new", 3) == 0;
2378+
int is_new = STRNCMP(p, "new", 3) == 0
2379+
|| STRNCMP(p, "_new", 4) == 0;
23792380

23802381
if (has_public)
23812382
{
@@ -2601,7 +2602,8 @@ ex_class(exarg_T *eap)
26012602
for (int i = 0; i < classfunctions.ga_len; ++i)
26022603
{
26032604
class_func = ((ufunc_T **)classfunctions.ga_data)[i];
2604-
if (STRCMP(class_func->uf_name, "new") == 0)
2605+
if (STRCMP(class_func->uf_name, "new") == 0
2606+
|| STRCMP(class_func->uf_name, "_new") == 0)
26052607
{
26062608
have_new = TRUE;
26072609
break;

0 commit comments

Comments
 (0)