help needed to understand handles to classdef methods

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

help needed to understand handles to classdef methods

John W. Eaton
Administrator
Could someone run the following test in a recent version of Matlab and
post the results?

Save the attached file as method_handle.m and then execute the following
commands at the Matlab prompt.  Some may generate errors.

x = method_handle ()
x.bar (1)
x.bar (2)
x.bar (3)
[h1, h2, h3] = x.get_handles ()
h1 (x, 1)
h2 (x, 2)
h3 (x, 3)
x.h1 (1)
x.h2 (2)
x.h3 (3)

This test is related to bug #45351.  I'm trying to understand both
syntax that is allowed and access restrictions.

Thanks,

jwe

method_handle.m (882 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: help needed to understand handles to classdef methods

khalil2535
Hope you got things as expected , that's matlab r2018a


=== code begin ===


>> x = method_handle ()

x =

  method_handle with no properties.

>> x.bar (1)

ans =

    'method_handle.foo: 1'

>> x.bar (2)

ans =

    'method_handle.foo: 2'

>> x.bar (3)

ans =

    'method_handle.foo: 3'

>> [h1, h2, h3] = x.get_handles ()

h1 =

  function_handle with value:

    @foo


h2 =

  function_handle with value:

    @(self,n)self.foo(n)


h3 =

  function_handle with value:

    @(~,n)self.foo(n)

>> h1 (x, 1)

ans =

    'method_handle.foo: 1'

>> h2 (x, 2)

ans =

    'method_handle.foo: 2'

>> h3 (x, 3)

ans =

    'method_handle.foo: 3'

>> x.h1 (1)
Undefined function 'foo' for input arguments of type 'double'.
 
>> x.h2 (2)
Not enough input arguments.

Error in method_handle>@(self,n)self.foo(n) (line 17)
      self.h2 = @(self, n) self.foo (n);
 
>> x.h3 (3)
Not enough input arguments.

Error in method_handle>@(~,n)self.foo(n) (line 18)
      self.h3 = @(~, n) self.foo (n);

=== code end ===



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html

Reply | Threaded
Open this post in threaded view
|

Re: help needed to understand handles to classdef methods

Richard Crozier
In reply to this post by John W. Eaton


On 25/02/2019 20:52, John W. Eaton wrote:

> x = method_handle ()
> x.bar (1)
> x.bar (2)
> x.bar (3)
> [h1, h2, h3] = x.get_handles ()
> h1 (x, 1)
> h2 (x, 2)
> h3 (x, 3)
> x.h1 (1)
> x.h2 (2)
> x.h3 (3)


R2018b

 >> x = method_handle ()
x.bar (1)
x.bar (2)
x.bar (3)
[h1, h2, h3] = x.get_handles ()
h1 (x, 1)
h2 (x, 2)
h3 (x, 3)
x.h1 (1)
x.h2 (2)
x.h3 (3)

x =

   method_handle with no properties.


ans =

     'method_handle.foo: 1'


ans =

     'method_handle.foo: 2'


ans =

     'method_handle.foo: 3'


h1 =

   function_handle with value:

     @foo


h2 =

   function_handle with value:

     @(self,n)self.foo(n)


h3 =

   function_handle with value:

     @(~,n)self.foo(n)


ans =

     'method_handle.foo: 1'


ans =

     'method_handle.foo: 2'


ans =

     'method_handle.foo: 3'

Undefined function 'foo' for input arguments of type 'double'.

 >>

The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.