To: vim_dev@googlegroups.com Subject: Patch 9.0.1155 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.1155 Problem: Cannot use a class as a type. Solution: Accept a class and interface name as a type. Files: src/vim9type.c, src/testdir/test_vim9_class.vim *** ../vim-9.0.1154/src/vim9type.c 2023-01-03 14:01:15.957750408 +0000 --- src/vim9type.c 2023-01-07 11:57:56.462919256 +0000 *************** *** 1272,1277 **** --- 1272,1301 ---- break; } + // It can be a class or interface name. + typval_T tv; + tv.v_type = VAR_UNKNOWN; + if (eval_variable(*arg, len, 0, &tv, NULL, + EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT) == OK) + { + if (tv.v_type == VAR_CLASS && tv.vval.v_class != NULL) + { + type_T *type = get_type_ptr(type_gap); + if (type != NULL) + { + // Although the name is that of a class or interface, the type + // uses will be an object. + type->tt_type = VAR_OBJECT; + type->tt_member = (type_T *)tv.vval.v_class; + clear_tv(&tv); + *arg += len; + return type; + } + } + + clear_tv(&tv); + } + if (give_error) semsg(_(e_type_not_recognized_str), *arg); return NULL; *** ../vim-9.0.1154/src/testdir/test_vim9_class.vim 2023-01-06 18:42:16.434674109 +0000 --- src/testdir/test_vim9_class.vim 2023-01-07 12:04:04.814984827 +0000 *************** *** 665,669 **** --- 665,718 ---- v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented') enddef + def Test_class_used_as_type() + var lines =<< trim END + vim9script + + class Point + this.x = 0 + this.y = 0 + endclass + + var p: Point + p = Point.new(2, 33) + assert_equal(2, p.x) + assert_equal(33, p.y) + END + v9.CheckScriptSuccess(lines) + + lines =<< trim END + vim9script + + interface HasX + this.x: number + endinterface + + class Point implements HasX + this.x = 0 + this.y = 0 + endclass + + var p: Point + p = Point.new(2, 33) + var hx = p + assert_equal(2, hx.x) + END + v9.CheckScriptSuccess(lines) + + lines =<< trim END + vim9script + + class Point + this.x = 0 + this.y = 0 + endclass + + var p: Point + p = 'text' + END + v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object but got string') + enddef + " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker *** ../vim-9.0.1154/src/version.c 2023-01-07 10:51:26.364797976 +0000 --- src/version.c 2023-01-07 11:46:59.538330765 +0000 *************** *** 697,698 **** --- 697,700 ---- { /* Add new patch number below this line */ + /**/ + 1155, /**/ -- If all you have is a hammer, everything looks like a nail. When your hammer is C++, everything begins to look like a thumb. -- Steve Hoflich, comp.lang.c++ /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///