Difference between revisions of "Classdef"

From Octave
Jump to navigation Jump to search
(removed fixed bug reports: #45351 (function handles),)
Line 52: Line 52:
 
Use "private" properties as workaround.
 
Use "private" properties as workaround.
  
 
* '''Function handles to package methods and static class methods.'''
 
For example if we have <code>+mypackage/myfunc.m</code>, creating a function handle as <code>fh = @mypackage.myfunc</code> won't work.
 
As a workaround, we can create an indirection using an anonymous function <code>fh = @(varargin) mypackage.myfunc(varargin{:})</code>.
 
Similarly for static class methods where <code>fh = @MyClass.myfunc</code> isn't yet supported. Another workaround for package function handles is to use <code>str2func</code>, e.g. <code>fh = str2func ("mypackage.myfunc")</code>.
 
 
A fuller example is also given below:
 
<source lang="octave">
 
classdef method_function_handle_test
 
 
%  properties
 
 
 
%  end
 
 
 
  properties (Hidden, SetAccess = protected)
 
    hfoo = [];% handle to function
 
  end
 
 
 
  methods
 
 
 
    function self = method_function_handle_test ()
 
      self.hfoo = @foo;   
 
    end
 
   
 
    function bar (self)
 
        self.hfoo (self);
 
    end
 
 
  end
 
 
 
  methods (Hidden, Access = protected)
 
    function foo (self)
 
      disp ('hello!');
 
    end
 
  end
 
 
end
 
</source>
 
Then running:
 
<source lang="text">
 
>> x = method_function_handle_test
 
error: @foo: no function and no method found
 
error: called from
 
    method_function_handle_test at line 17 column 17
 
stopped in <****>/scratch/mfiles/octave_tests/method_function_handle_test.m at line 17
 
17:      self.hfoo = @foo;
 
</source>
 
 
See {{bug|45351}}
 
  
 
* '''Defining [http://www.mathworks.com/help/matlab/matlab_oop/specifying-methods-and-functions.html#br2la89 local functions] in the same classdef-file is not working.'''  
 
* '''Defining [http://www.mathworks.com/help/matlab/matlab_oop/specifying-methods-and-functions.html#br2la89 local functions] in the same classdef-file is not working.'''  

Revision as of 05:57, 12 April 2019

This page is a stub. We should begin documenting what classdef is implemented and what is not.

Not Supported

  • enumeration

See #44582 and help implementing it.


  • build-in class as superclass
classdef nonsense < uint32

end

See #44035

Example:

classdef MyClass < handle
end

c = MyClass();
cc = [c, c];   % won't work

For now you can use a cell-array of objects instead:

cc = {c, c};   % ok

Also see #44665

Example:

classdef MyClass < handle
    properties (SetAccess = immutable)
        x
    end
    methods
        function obj = MyClass()
            x = rand();
        end
    end
end

Use "private" properties as workaround.


For example, the following code gives a syntax/parse error:

classdef MyClass
    methods
        function obj = MyClass()
            myfunc()
        end
    end
end

function myfunc()
    disp('myfunc')
end

See #41723.

  • debugging in classdef methods (and +package directory functions)

Breakpoints cannot currently be set in classdef methods from the gui, but only from the command line:

dbstop @aclass\amethod

See #45404

  • Saving/reading classdef objects to/from (.mat) file

Supported

  • methods
    • static
    • private
  • properties
    • SetAccess (public/private/protected)

Open Bug Reports

Issues with basic classdef functionality

  • 51659 Calling 'methods' on self causes syntax error
  • 48682 print_usage fails within classdef block
  • 49434 which returns "built-in function" for classdef m-file
  • 48041 classdef: `help myclass` messes up `help @myclass/method`
  • 43047 help() does not see classdef files
  • 42620 exist() does not use "class" argument
  • 53874 doc_cache_create doesnt handle classdef documentation
  • 52096 meta.class.fromName throws error when class name not found.
  • 51377 Parse error for abstract methods
  • 44665 error in concatenation of classdef objects
  • 44582 classdef: missing support for enumeration
  • 48693 classdef subsref method is not called with correct nargout value

Classdef and +package directories

  • 54941 interpreter cannot find methods in files of classdefs in packages

Arrays of classdef objects

  • 53906 Cannot make an object array with square brackets
  • 47755 Access to object arrays
  • 47241 classdef: assigning property of handle object in object array constructs new object

I/O issues

  • 45833 support load/save of classdef objects

Debugger

  • 46451 unable to set breakpoints within classdef classes
  • 45404 Breakpoints cannot be set in classdef methods or +package function files

Lower-priority issues

  • 55488 Invalid use of colon char as classdef function's argument when subsref is overwritten
  • 54966 Error when assigning array to an object implementing subsasgn() subscripted using "{}"
  • 54028 copy of non-handle class instance is not deep
  • 53811 cellfun does not find overloaded function with function name argument
  • 52989 classdef: missing error messages on multiply defined properties
  • 52614 setting properties of classdef object during construction with inheritance
  • 52582 Dependent constant properties in classdef errors: no such file
  • 52123 Indirect memory leak in cdef_manager::initialize ()
  • 51285 max_recursion_depth error in classdef constructor
  • 50395 subclassing a class that is also defined as a variable fails
  • 50011 failure to report error on conflicting methods for classdef
  • 49379 classdef constructor: .argn. loses first argument, inputname(n) returns inputname(n+1)
  • 55810 sizeof() and whos() returns 0 bytes for classdef objects
  • 45893 classdef properties are not reloaded when file is updated
  • 44934 classdef parser prints extra newline when failing to parse bad classdef file
  • 44643 classdef handle object can go into an recursive loop with isequal(obj1,obj2) if both are self-referential
  • 44035 classdef is unable to to subclass double

Documentation

  • 50729 Improve OOP documentation
  • 47908 Octave:classdef-to-struct not documented in warning_ids.m
  • 44590 More documentation for the current status of classdef implementation

Classdef examples in the wild