Classdef: Difference between revisions

From Octave
Jump to navigation Jump to search
Line 1: Line 1:
This page is a stub. We should begin documenting what classdef is implemented and what is not.
This page is a stub. We should begin documenting what classdef is implemented and what is not.


==== not supported ====
=== Not Supported ===


* debugging in classdef methods (and +package directory functions)
* '''debugging in classdef methods (and +package directory functions)'''


Breakpoints cannot currently be set in classdef methods (or at least they are ignored). They also can't be set in functions in +package directories (which is a related issue).
Breakpoints cannot currently be set in classdef methods (or at least they are ignored). They also can't be set in functions in +package directories (which is a related issue).


* enumeration
 
* '''enumeration'''
 


* build-in class as superclass  
* build-in class as superclass  
Line 16: Line 18:
</source>
</source>


* [http://www.mathworks.com/help/matlab/matlab_oop/redefining-concatenation-for-your-class.html concatenating objects] into [http://www.mathworks.com/help/matlab/matlab_oop/initialize-object-arrays.html array of objects]. Example:
 
* '''[http://www.mathworks.com/help/matlab/matlab_oop/redefining-concatenation-for-your-class.html concatenating objects] into [http://www.mathworks.com/help/matlab/matlab_oop/initialize-object-arrays.html array of objects].'''
 
Example:
<source lang="octave">
<source lang="octave">
classdef MyClass < handle
classdef MyClass < handle
Line 30: Line 35:
</source>
</source>


* [http://www.mathworks.com/help/matlab/matlab_oop/mutable-and-immutable-properties.html Immutable property] set access. Example:
 
* '''[http://www.mathworks.com/help/matlab/matlab_oop/mutable-and-immutable-properties.html Immutable property] set access. Example:'''
<source lang="octave">
<source lang="octave">
classdef MyClass < handle
classdef MyClass < handle
Line 46: Line 52:
Use "private" properties as workaround.
Use "private" properties as workaround.


* Function handles to package methods and static class methods.
 
* '''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.
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>.
As a workaround, we can create an indirection using an anonymous function <code>fh = @(varargin) mypackage.myfunc(varargin{:})</code>.
Line 95: Line 102:




* 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. For example, the following code gives a syntax/parse error:
* '''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.'''
For example, the following code gives a syntax/parse error:
<source lang="octave">
<source lang="octave">
classdef MyClass
classdef MyClass

Revision as of 10:25, 25 June 2015

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

Not Supported

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

Breakpoints cannot currently be set in classdef methods (or at least they are ignored). They also can't be set in functions in +package directories (which is a related issue).


  • enumeration


  • build-in class as superclass
classdef nonsense < uint32

end


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


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

Use "private" properties as workaround.


  • Function handles to package methods and static class methods.

For example if we have +mypackage/myfunc.m, creating a function handle as fh = @mypackage.myfunc won't work. As a workaround, we can create an indirection using an anonymous function fh = @(varargin) mypackage.myfunc(varargin{:}). Similarly for static class methods where fh = @MyClass.myfunc isn't yet supported.

A fuller example is also given below:

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

Then running:

>> 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;


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

supported

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

Classdef examples in the wild