Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle display of java-wrapper objects with empty j contents? #1

Open
apjanke opened this issue Apr 17, 2020 · 2 comments
Open

Comments

@apjanke
Copy link
Member

apjanke commented Apr 17, 2020

I'm building most of my classes in MSch as Matlab classdef wrappers around the corresponding Java objects. The pattern looks like this:

classdef Identity < handle
  
  properties (SetAccess = private, Hidden = true)
    % The underlying com.jcraft.jsch.Identity Java object
    j
  end
  properties (Dependent)
    name
    algorithmName
    isEncrypted
  end
  
  methods
    
    function this = Identity(jIdentity)
      if nargin == 0
        return
      end
      mustBeA(jIdentity, 'com.jcraft.jsch.Identity');
      this.j = jIdentity;
    end
    
    function out = get.name(this)
      out = string(this.j.getName);
    end
    
    function out = get.algorithmName(this)
      out = string(this.j.getAlgName);
    end
    
    function out = get.isEncrypted(this)
      out = this.j.isEncrypted;
    end

  end

end

That j property is intended to hold the wrapped Java object.

But it's necessary to allow for Matlab objects that have an empty j value: it's not possible in all cases to construct appropriate default underlying Java object values efficiently or at all. And we need to be able to construct "null" or "uninitialized" Matlab objects, for the cases when we need to pre-allocate an array (e.g. idents = repmat(msch.Identity, [1 n]);) or construct a default object instance to conform to declarative type constraint requirements on properties in other objects.

For these "uninitialized" objects, j is [], so the get.XXX(this) methods will fail, because they try to do a method call on [], which is a double that doesn't support those methods. And the default object disp()/display() for scalar objects will call those methods, because it tries to call those methods. What happens is that those properties appear to not exist in the object display (it doesn't throw an error).

Should I do something to handle this more gracefully?

  • Custom disp() that displays " (uninitialized)" for uninitialized objects?
  • Alter the get.* methods to return default values when j is empty?

I think this is just a cosmetic issue, since display doesn't actually raise an error.

@apjanke
Copy link
Member Author

apjanke commented Apr 17, 2020

Oh, you know what: I should probably call these "missing" instead of "uninitialized", and have them respect Matlab missing conversions and semantics.

@apjanke
Copy link
Member Author

apjanke commented Apr 17, 2020

Here's an idea from the Matlab Discord:

  properties (SetAccess = private, Hidden)
    j {mustBeAOrMissing(j, 'com.jcraft.jsch.Identity')} = missing
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant