nargout for methods

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
jjg
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

nargout for methods

jjg
This post was updated on .
Hi all,

I have a stable library (MVPoly in case anyone is interested, http://soliton.vm.bytemark.co.uk/pub/jjg/en/code/mvpoly/) which defines a classdef-style class with all the methods in a @ directory, those methods have their signatures in the classdef file.  All is well with that.

I have some unstable code which I'd like to add to the library locally.  I create a package with a different name but with the same name for the @ directory and put my new method in there.  

  function [M, v, nevals] = unstable(p, other, args)
    yada yada

This new method is accessible from instances of the class, I can do

  p = MVPoly()
  M = p.unstable()

and that works, but, if I try

  [M, v, nevals] = p.unstable()

I get

  error: element number 2 undefined in return list

and indeed, if I put some debugging code in my unstable method, nargout is always 1, no matter how many out-arguments are present.  I suspect that this is because my unstable method does not have a signature in the stable library classdef file, but if I add one then the behaviour stays the same.  I then suspect that the "scope" of the signature does not extend to my second @ directory and I start to run out of ideas.

So does anyone have an idea as to how I could add a method with multiple return values to an existing class without integrating the code into that class, "re-open" the class to add methods if you will.  I suspect this is not possible but I'd be grateful if someone could confirm that (and delighted if there is some kind of trick possible)

Cheers

Jim
jjg
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: nargin for methods

jjg
In the above, one should read "nargout" for "nargin"
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: nargout for methods

mmuetzel
In reply to this post by jjg
Hi Jim,

have you considered inheriting from mvpoly and add your new methods in the subclass. Something like this might work:

classdef myMVPoly < MVPoly
  methods
    function [M, v, nevals] = unstable(p, other, args)
      yada yada
    endfunction
  endmethods
endclassdef

I am not sure whether the subclass within a package can have the same name as the superclass outside of any packages.

When you write "package", do you mean that you store all files in a "+myPackage" folder? In this case, the constructor of your class should be called by:
p = myPackage.MVPoly();
Or whatever the name of the "+" folder.

Cheers
Markus
jjg
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: nargout for methods

jjg
Ah, interesting -- I'll give that a try.  Ideally I would like to keep the original name (I have thoughts of distributing the package separately, since the old stable package is only m-files, while the new one is a C extension (and the "unstable" function is just a wrapper around that C function), so installation requires work that I'd not like to force on the general user).

And by "package" I mean rather an "Octave package" as in "pkg load mvpoly"
jjg
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: nargout for methods

jjg
In reply to this post by mmuetzel
It seems that

   classdef MVPoly < MVPoly

does not work, (unsurprisingly) an infinite recursion happens

  error: class not found: MVPoly
  error: class not found: MVPoly
    :

well, not quite infinite, about a hundred of those
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: nargout for methods

siko1056
jjg wrote
It seems that

   classdef MVPoly < MVPoly

does not work
Maybe you did not apply Markus idea rigorously enough. He suggested:

classdef myMVPoly < MVPoly

Notice the difference between "myMVPoly" and "MVPoly".

HTH,
Kai
jjg
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: nargout for methods

jjg
Thanks for the comment Kai, but I was really looking a way to keep the same name for the class, since I want to extend the existing stable class with extra functionality.  Otherwise, adding this extra functionality would require the user to change the class name in all of their existing scripts, which seems like a bad user experience.

It seems like this is not possible in Octave, so I will add a stub in the stable class which does an "exists" on the extra functionality and points the user to the extra package if not present ...

Thanks to all who took time to look at this
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: nargout for methods

Mike Miller-4
On Thu, Jul 20, 2017 at 09:02:36 -0700, jjg wrote:
> Thanks for the comment Kai, but I was really looking a way to keep the same
> name for the class, since I want to extend the existing stable class with
> extra functionality.  Otherwise, adding this extra functionality would
> require the user to change the class name in all of their existing scripts,
> which seems like a bad user experience.
>
> It seems like this is not possible in Octave, so I will add a stub in the
> stable class which does an "exists" on the extra functionality and points
> the user to the extra package if not present ...

If I'm understanding what you want, it does work for me.

I can create an external method for an object in a separate directory,
and nargout works correctly.

Maybe you could try reducing what you've tried to a simpler example, or
give a complete demonstration that someone can try to reproduce.

Here's what I've tested:

    dir1/@myclass/disp.m
    dir1/@myclass/myclass.m

    dir2/@myclass/unstable.m with function definition
    function [a, b, c] = unstable (self)

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
jjg
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: nargout for methods

jjg
Hi Mike,

Thanks for your note.  After reading it, I created a minimal example class to exhibit the problem and find the same as you, everything works fine.  After a lot of debugging (a bisection on my stable class, deleting things until it started working properly) I find that the problem was that my class overrides subsref, and used a singular return value.  Once I modified it to return varargout instead, everything started working.

In case anyone happens across a similar issue, the commit which fixed the issue is here.

Thank you to all who took the time to look at this, very much appreciated.

Cheers

Jim
Loading...