summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2023-01-14 13:12:06 +0000
committerBram Moolenaar <Bram@vim.org>2023-01-14 13:12:06 +0000
commit24a8d06d7f4db0865f374ced2f4d4b57cbc5b9e4 (patch)
tree35fbe252c9cebe7cbddd33892e079f3966af402c
parent034c350207931a7ff57c76536d4703f6da595919 (diff)
patch 9.0.1198: abstract class not supported yetv9.0.1198
Problem: Abstract class not supported yet. Solution: Implement abstract class and add tests.
-rw-r--r--src/errors.h2
-rw-r--r--src/testdir/test_vim9_class.vim35
-rw-r--r--src/version.c2
-rw-r--r--src/vim9class.c31
4 files changed, 58 insertions, 12 deletions
diff --git a/src/errors.h b/src/errors.h
index de1ae4bd03..3b6307f0ba 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -3440,4 +3440,6 @@ EXTERN char e_using_super_not_in_class_function[]
INIT(= N_("E1357: Using \"super\" not in a class function"));
EXTERN char e_using_super_not_in_child_class[]
INIT(= N_("E1358: Using \"super\" not in a child class"));
+EXTERN char e_cannot_define_new_function_in_abstract_class[]
+ INIT(= N_("E1359: Cannot define a \"new\" function in an abstract class"));
#endif
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim
index a6caad2fce..4a2b87c740 100644
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -1014,5 +1014,40 @@ def Test_class_import()
v9.CheckScriptSuccess(lines)
enddef
+def Test_abstract_class()
+ var lines =<< trim END
+ vim9script
+ abstract class Base
+ this.name: string
+ endclass
+ class Person extends Base
+ this.age: number
+ endclass
+ var p: Base = Person.new('Peter', 42)
+ assert_equal('Peter', p.name)
+ assert_equal(42, p.age)
+ END
+ v9.CheckScriptSuccess(lines)
+
+ lines =<< trim END
+ vim9script
+ abstract class Base
+ this.name: string
+ endclass
+ class Person extends Base
+ this.age: number
+ endclass
+ var p = Base.new('Peter')
+ END
+ v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Base": new(')
+
+ lines =<< trim END
+ abstract class Base
+ this.name: string
+ endclass
+ END
+ v9.CheckScriptFailure(lines, 'E1316:')
+enddef
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
diff --git a/src/version.c b/src/version.c
index 52eabb5807..763c79d91c 100644
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1198,
+/**/
1197,
/**/
1196,
diff --git a/src/vim9class.c b/src/vim9class.c
index d66fa3ecc4..8dcd92b6eb 100644
--- a/src/vim9class.c
+++ b/src/vim9class.c
@@ -204,17 +204,6 @@ ex_class(exarg_T *eap)
{
int is_class = eap->cmdidx == CMD_class; // FALSE for :interface
- if (!current_script_is_vim9()
- || (cmdmod.cmod_flags & CMOD_LEGACY)
- || !getline_equal(eap->getline, eap->cookie, getsourceline))
- {
- if (is_class)
- emsg(_(e_class_can_only_be_defined_in_vim9_script));
- else
- emsg(_(e_interface_can_only_be_defined_in_vim9_script));
- return;
- }
-
char_u *arg = eap->arg;
int is_abstract = eap->cmdidx == CMD_abstract;
if (is_abstract)
@@ -225,6 +214,18 @@ ex_class(exarg_T *eap)
return;
}
arg = skipwhite(arg + 5);
+ is_class = TRUE;
+ }
+
+ if (!current_script_is_vim9()
+ || (cmdmod.cmod_flags & CMOD_LEGACY)
+ || !getline_equal(eap->getline, eap->cookie, getsourceline))
+ {
+ if (is_class)
+ emsg(_(e_class_can_only_be_defined_in_vim9_script));
+ else
+ emsg(_(e_interface_can_only_be_defined_in_vim9_script));
+ return;
}
if (!ASCII_ISUPPER(*arg))
@@ -493,6 +494,12 @@ early_ret:
{
char_u *name = uf->uf_name;
int is_new = STRNCMP(name, "new", 3) == 0;
+ if (is_new && is_abstract)
+ {
+ emsg(_(e_cannot_define_new_function_in_abstract_class));
+ success = FALSE;
+ break;
+ }
garray_T *fgap = has_static || is_new
? &classfunctions : &objmethods;
// Check the name isn't used already.
@@ -826,7 +833,7 @@ early_ret:
have_new = TRUE;
break;
}
- if (is_class && !have_new)
+ if (is_class && !is_abstract && !have_new)
{
// No new() method was defined, add the default constructor.
garray_T fga;