Skip to content

Commit 79e473f

Browse files
committed
Change ordering of classdef subsref to resolve to properties before methods (bug #67362).
* cdef-object.cc (cdef_object_scalar::subsref): On dot-indexing, attempt to resolve to a property first before resolving to a method. * etc/NEWS.11.md: Add note about change in behavior to NEWS file. * test/classdef/@class_bug67362/class_bug67362.m, test/classdef/@class_bug67362/shared_name.m: Add new files for test. * test/classdef/classdef.tst: Add test that checks the behavior. * test/classdef/module.mk: Add new files to dist tarball.
1 parent f9c3a8a commit 79e473f

File tree

6 files changed

+52
-20
lines changed

6 files changed

+52
-20
lines changed

etc/NEWS.11.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Summary of important user-visible changes for version 11 (yyyy-mm-dd):
3232
- The `roots` function now accepts only a numeric argument. Convert any
3333
non-numeric input to a numeric value with `double()`.
3434

35+
- On dot-indexing of `classdef` objects, `subsref` now resolves to looking for
36+
a property first, and if a property is not found, then it looks for a method.
37+
3538
### Graphical User Interface
3639

3740
- The GUI now uses scalable SVG icons for beautiful display at any size.

libinterp/octave-value/cdef-object.cc

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,29 @@ cdef_object_scalar::subsref (const std::string& type,
567567
{
568568
std::string name = (idx.front ())(0).string_value ();
569569

570-
cdef_method meth = cls.find_method (name);
570+
cdef_property prop = cls.find_property (name);
571571

572-
if (meth.ok ())
572+
if (prop.ok ())
573573
{
574+
if (prop.is_constant ())
575+
retval(0) = prop.get_value (true, "subsref");
576+
else
577+
{
578+
m_count++;
579+
retval(0) = prop.get_value (cdef_object (this),
580+
true, "subsref");
581+
}
582+
583+
skip = 1;
584+
}
585+
586+
if (skip == 0)
587+
{
588+
cdef_method meth = cls.find_method (name);
589+
590+
if (! meth.ok ())
591+
error ("subsref: unknown method or property: %s", name.c_str ());
592+
574593
// If the method call is followed by another index operation,
575594
// the number of outputs of the call will be 1.
576595
int nargout_mtd = (type.length () > 2
@@ -600,24 +619,6 @@ cdef_object_scalar::subsref (const std::string& type,
600619
}
601620
}
602621

603-
if (skip == 0)
604-
{
605-
cdef_property prop = cls.find_property (name);
606-
607-
if (! prop.ok ())
608-
error ("subsref: unknown method or property: %s", name.c_str ());
609-
610-
if (prop.is_constant ())
611-
retval(0) = prop.get_value (true, "subsref");
612-
else
613-
{
614-
m_count++;
615-
retval(0) = prop.get_value (cdef_object (this),
616-
true, "subsref");
617-
}
618-
619-
skip = 1;
620-
}
621622
break;
622623
}
623624

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
classdef class_bug67362
2+
## Test class with property that shares the name with a method in a @folder
3+
4+
properties
5+
shared_name = 42; # property shares name with method in @folder
6+
endproperties
7+
8+
methods
9+
10+
function obj = class_bug67362 ()
11+
endfunction
12+
13+
endmethods
14+
15+
endclassdef
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function retval = shared_name (obj)
2+
## This method shares a name with a property.
3+
4+
retval = 1;
5+
6+
endfunction

test/classdef/classdef.tst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,8 @@
261261

262262
%!error <cannot reshape scalar .* to .* array>
263263
%! reshape (value_class (), [3, 2]);
264+
265+
## properties take precedence over methods with the same name
266+
%!test <*67362>
267+
%! obj = class_bug67362 ();
268+
%! assert (obj.shared_name, 42);

test/classdef/module.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
classdef_TEST_FILES = \
22
%reldir%/@class_bug62802/amethod.m \
33
%reldir%/@class_bug62802/class_bug62802.m \
4+
%reldir%/@class_bug67362/class_bug67362.m \
5+
%reldir%/@class_bug67362/shared_name.m \
46
%reldir%/bug_60763.m \
57
%reldir%/class_bug50011_1.m \
68
%reldir%/class_bug50011_1A.m \

0 commit comments

Comments
 (0)