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)
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()
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)
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.
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"
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 ...
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:
dir2/@myclass/unstable.m with function definition
function [a, b, c] = unstable (self)
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.