|
|
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.''' |