Need test for handles to classdef methods run in Matlab

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

Need test for handles to classdef methods run in Matlab

John W. Eaton
Administrator
I'm attaching a simple classdef file, mycls.m, and need the following
tests run with it in a recent version of Matlab:


obj = mycls ();

obj.meth (1)

mycls.smeth (1)

obj.smeth (1);

fh = @obj.meth;
fh(2)

fh = @mycls.smeth;
fh (2)

fh = @obj.smeth;
fh (2)


You should run the tests individually instead of in a script as there
may be errors and I need to see all the results, not just up to the
first error.

Thanks,

jwe

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

Re: Need test for handles to classdef methods run in Matlab

PhilipNienhuis
John W. Eaton wrote
> I'm attaching a simple classdef file, mycls.m, and need the following
> tests run with it in a recent version of Matlab:
> <snip>
> You should run the tests individually instead of in a script as there
> may be errors and I need to see all the results, not just up to the
> first error.

Matlab r2019a prerelease:

>> format compact
>> obj = mycls ();
>> obj.meth (1)
meth
varargin =
  1×2 cell array
    {1×1 mycls}    {[1]}
>> mycls.smeth (1)
smeth
varargin =
  1×1 cell array
    {[1]}
>> obj.smeth (1)
smeth
varargin =
  1×1 cell array
    {[1]}
>> fh = @obj.meth;
>> fh(2)
meth
varargin =
  1×2 cell array
    {1×1 mycls}    {[2]}
>> fh = @mycls.smeth;
>> fh (2)
smeth
varargin =
  1×1 cell array
    {[2]}
>> fh = @obj.smeth;
>> fh (2)
smeth
varargin =
  1×1 cell array
    {[2]}
>>


Philip



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

Reply | Threaded
Open this post in threaded view
|

Re: Need test for handles to classdef methods run in Matlab

John W. Eaton
Administrator
On 3/23/19 5:31 AM, PhilipNienhuis wrote:

> Matlab r2019a prerelease:
>
>>> format compact
>>> obj = mycls ();

>>> fh = @obj.meth;
>>> fh(2)
> meth
> varargin =
>    1×2 cell array
>      {1×1 mycls}    {[2]}

>>> fh = @obj.smeth;
>>> fh (2)
> smeth
> varargin =
>    1×1 cell array
>      {[2]}

Thanks.

Now I need to know one more thing.  In expressions like the ones shown
above, is the value of obj captured when the function handle is created,
or is it just a name that is used for lookup later?  To test that, maybe
the following will give some clue.  If you define the additional function

   function [fh1, fh2] = foo ()
     obj = mycls ();
     fh1 = @obj.meth;
     fh2 = @obj.smeth;
   end

and then execute

   clear all
   [fh1, fh2] = foo ();
   fh1 (1)
   fh2 (2)

what happens?  Another test:

   clear all
   obj = mycls ();
   fh1 = @obj.meth;
   fh2 = @obj.smeth;
   clear obj
   fh1 (1)
   fh2 (2)

Could someone run these tests in a recent version of Matlab and report
the results here?  I'm attaching mycls.m and foo.m to this message.

jwe



foo.m (95 bytes) Download Attachment
mycls.m (220 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Need test for handles to classdef methods run in Matlab

nrjank
matlab 2018b.  diary attached

fntests2.txt (890 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Need test for handles to classdef methods run in Matlab

John W. Eaton
Administrator
On 3/24/19 2:21 AM, Nicholas Jankowski wrote:
> matlab 2018b.  diary attached

Thanks.

So the results you posted appear to indicate that the value of the
object is captured at the time that the handle is created.  That means
more work is needed to create a function handle than just "store the
name and the current scope".  It's not clear to me exactly when lookup
will happen early or be deferred to the evaluation of the handle
(calling a function through the handle).  But I think I can make this
feature work, and for now that's all I want to do and I'm going to have
all other function lookups happen at the point of evaluation for now,
until someone reports that not working properly in some case.

jwe