bugGNU Octave - Bugs: bug #59737, end keyword not passed to...

 
 

bug #59737: end keyword not passed to overloaded subsref function within custom classdef

Submitted by:  None
Submitted on:  Sun 20 Dec 2020 05:01:19 AM UTC  
 
Category:  Documentation Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Documentation
Status:  Confirmed Assigned to:  None
Originator Name:  Originator Email:  -email is unavailable-
Open/Closed:  Open Release:  6.1.0
Operating System:  Any

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Sun 20 Dec 2020 11:02:38 PM UTC, comment #2: 

Please change this bug report to a request to change documentation. I agree this bug as reported is not a bug. Is it possible to add a sentence like:

If you would like to use the end keyword with subsref you will also need to overload end.

and/or

add end to the see also list

See also: subsasgn, substruct, end.

The reason I overloaded every size function I could think of was to see if one of them was being used by end. There was no hint in the documentation, the syntax highlighter or from my google searches (I was not searching overload end. I was searching my problem.) that I could overload end.

The working as expected class with overloaded end:

classdef cantuseend
  properties
    value;
  endproperties
  methods
    function obj=cantuseend(a)
      obj.value=a;
    endfunction
    function obj=subsref(a,s)
      if strcmp(s.type,"()")
        obj=cantuseend(subsref(a.value,s));
        s
      else
        obj=builtin('subref',a,s);
      end
    endfunction
    function display(a)
      disp(a.value);
    endfunction
    function lastindex=end(a,k,n)
      lastindex=size(a,k);
    endfunction
    function varargout=size(a,varargin)
      nout=nargout;
      if nout<=1
        varargout={size(a.value,varargin{:})};
      else
        varargout=nthargout(1:max(nargout,1),@size,a.value,varargin{:});
      end
    end
  endmethods
endclassdef

Anonymous
Sun 20 Dec 2020 07:43:40 PM UTC, comment #1: 

This is not an error, it is the expectected behaviour of END when used as an index expression. You will get the same result in Matlab (I just tried it with Matlab 2020a). When used in an index expression, END acts as a method that is called on the object being indexed. You have more information here:

https://es.mathworks.com/help/matlab/matlab_oop/object-end-indexing.html

Fernando <tutissanalio>
Sun 20 Dec 2020 05:01:19 AM UTC, original submission:  

Problem Description

The end keyword is not passed to an overloaded subsref function.

How to reproduce

Copy the following into cantuseend.m

classdef cantuseend
  properties
    value;
  endproperties
  methods
    function obj=cantuseend(a)
      obj.value=a;
    endfunction
    function obj=subsref(a,s)
      if strcmp(s.type,"()")
        obj=cantuseend(subsref(a.value,s));
        s
      else
        obj=builtin('subref',a,s);
      end
    endfunction
    function display(a)
      disp(a.value);
    endfunction
    function varargout=size(a,varargin)
      nout=nargout;
      if nout<=1
        varargout={size(a.value,varargin{:})};
      else
        varargout=nthargout(1:max(nargout,1),@size,a.value,varargin{:});
      end
    end
    function v=columns(a)
      v=columns(a.value);
    endfunction
    function v=rows(a)
      v=rows(a.value);
    endfunction
    function v=numel(a,varargin)
      v=numel(a.value,varargin{:});
    endfunction
    function v=ndims(a)
      v=ndims(a.value);
    end
    function v=length(a)
      v=length(a.value);
    end
  endmethods
endclassdef

At the command prompt type:

q=cantuseend(rand(4,1))
q(2:4)%works as expected
q(2:end)%Expected the above output got 1 by 0 empty result
q(end)%returns the first element not the last

I am printing what is in the sub-reference structure every time the function is called. For the second call I get an empty subs for the third call I get the number one within subs.

Suggested Fixes

Pass through a token which can be interpreted as end - even better if this token can be interpreted by builtin functions.

Use the size method (or another method, document which one) of the classdef object to convert end into the correct index. Give an error if the size method has not been overloaded.

Raise an error telling the user end is not supported for custom objects and tell the user to pass the last index of the object instead of end. This can't currently be done within an overloaded subsref function as the input looks valid. Without an error being thrown this bug could result in undetected incorrect results and code that is very difficult to debug because without looking at each output and finding this bug the code will look correct.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Updated the item)
  • -email is unavailable- added by tutissanalio (Posted a comment)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-12-21 mmuetzel CategoryOctave Function => Documentation
        Item GroupIncorrect Result => Documentation
        StatusNone => Confirmed
        Summaryend keyword not passed to oveloaded subsfef function within custom classdef => end keyword not passed to overloaded subsref function within custom classdef

    Back to the top


    Powered by Savane 3.5