bugGNU Octave - Bugs: bug #48592, function char on java objects

 
 

bug #48592: function char on java objects

Submitter:  Ernst Reissner <ernstreissner>
Submitted:  Fri 22 Jul 2016 12:17:43 AM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Need Info Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 19 Jul 2018 05:34:10 AM UTC, comment #4: 

Here's what that doco page currently says (from https://www.mathworks.com/help/matlab/matlab_external/handling-data-returned-from-java-methods.html):

------------------------

Convert to MATLAB Strings

To convert java.lang.String objects and arrays to MATLAB strings or character vectors, use the MATLAB string or char function.

If the object specified in the MATLAB function is not an instance of the java.lang.String class, then MATLAB checks its class definition for a toString or a toChar method.

------------------------

So, the pseudocode is, I think:


if (x instanceof java.lang.String)
  return toOctaveChar(x);
else if (x hasmethod toChar)
  return toOctaveChar(x.toChar());
else
  return toOctaveChar(x.toString());


There is no "else throw error", because every Java object has a toString method: it's defined in the root superclass java.lang.Object. Subclasses can just provide customizations of it.

My testing agrees with this. In Matlab, `char(jobj)` succeeds on things that clearly do not have a toChar method. In fact, it succeeds on every Java object I could think of.


>> m = java.util.HashMap
m =
{}
>> char(m)
ans =
    '{}'
>> class(ans)
ans =
    'char'


That doco page also has a sidebar that says this: "If the class of the object is not java.lang.String and it does not implement a toChar method, then the char function displays an error message." I think they may have made a small mistake, and meant to say "a toString or toChar method". And since every Java object has a toString method, that sidebar is never actually relevant. Perhaps it is left over from an older version of Matlab that did not use toString.

Octave's current behavior seems correct and Matlab-consistent to me. (Except for the UTF-8 vs UTF-16 issue of converting Java char[] arrays, per https://savannah.gnu.org/bugs/index.php?54170, but that's a separate issue.)

Andrew Janke <apjanke>
Fri 19 Aug 2016 10:04:18 AM UTC, comment #3: 

what kind of information do you require to confirm or to reject this bugreport?

Ernst Reissner <ernstreissner>
Fri 29 Jul 2016 01:33:18 AM UTC, comment #2: 

I read: if an array of strings, the result is an array of chars.
Well not clear whether String[] is mapped onto char[][] or onto char[].

The rest seems to me as you describe it.


Ernst Reissner <ernstreissner>
Fri 22 Jul 2016 04:11:13 PM UTC, comment #1: 

Do you have access to Matlab to test?

The way I read the reference page is (pardon my Java pseudocode):


if (x instanceof java.lang.String)
  return x.toString();
else if (x hasmethod toChar)
  return x.toChar();
else
  throw error


Do you agree?

Some examples of java classes to demonstrate this would be helpful, if you can suggest some classes from the standard library that show: a subclass of java.lang.String, a class that is not a subclass of java.lang.String but has a toString, and a class that has a toChar but not a toString.

Mike Miller <mtmiller>
Group Member
Fri 22 Jul 2016 12:17:43 AM UTC, original submission:  


http://de.mathworks.com/help/matlab/matlab_external/handling-data-returned-from-a-java-method.html

shows that unless a java object implements toChar,
method char(m) applied to java object m
shall result in an error.

what it seems to do is to invoke m.toString()
which is wrong.

Ernst Reissner <ernstreissner>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by apjanke (Posted a comment)
  • -email is unavailable- added by ernstreissner (Submitted the item)
  • -email is unavailable- added by ernstreissner
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-02-22 mtmiller Carbon-CopyRemoved 80942 -
    2016-07-22 mtmiller CategoryNone Interpreter
        Item GroupNone Matlab Compatibility
        StatusNone Need Info
        Release4.0.2 dev
    2016-07-22 ernstreissner Carbon-Copy- Added -email is unavailable-

    Back to the top

    Powered by Savane 3.13-5884.
    Corresponding source code