bugGNU Octave - Bugs: bug #50367, default constructor for legacy...

 
 

bug #50367: default constructor for legacy class objects is not called when initializing an array

Submitter:  Ernst Reissner <ernstreissner>
Submitted:  Tue 21 Feb 2017 12:59:39 PM UTC
   
 
Category:  Interpreter Severity:  1 - Wish
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  Ernst Reissner Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Wed 29 Mar 2017 09:12:32 AM UTC, comment #8: 

Please, this is not a wish, it is a bug.

I think as long as the workaround is not documented,
it is normal priority.
So either documentation or fix would be necessary.


Ernst Reissner <ernstreissner>
Wed 08 Mar 2017 09:31:57 AM UTC, comment #7: 

question: where in the manual is classdef described?
I know that there is a way in matlab...

I would not say that this is a wish.
It is a deviation from matlab and it is a bug.

Ok, priority is low because there is a workaround,
but on the other hand,
if the workaround is not documented,
then priority is not low.

Also, I think behavior should not depend on notation
and so there is something to do anyway.

What do you think about this?

Ernst Reissner <ernstreissner>
Tue 07 Mar 2017 10:01:53 PM UTC, comment #6: 

I also appreciate a lot that there is now a workaround.
On the other hand, I would expect,
that for both types of declaration,
behind the scenes the same code runs
and so initialization works either with both notations or with none.
So I think , rework the code would be fine.

Ernst Reissner <ernstreissner>
Fri 03 Mar 2017 05:15:50 PM UTC, comment #5: 

Thanks for testing with a classdef defined class, that's a good observation that it actually is working correctly with modern class definitions.

Your testing does show that it works with both old-style and new-style classes in Matlab, right? So it might still be a low priority task that someone might want to support this in Octave, since there are still bugs in classdef that may prevent users from adopting it at this point. I would suggest to keep this open but low priority if someone wants to ensure that old style classes are still working compatibly.

Mike Miller <mtmiller>
Group Member
Fri 03 Mar 2017 11:54:02 AM UTC, comment #4: 

I think it is not worth fixing. MATLAB disbands from the `obj = class (obj, 'cname')` syntax in favor of classdef, that works like a charm for you example, that was flawed by the way:

Legacy class in "@xxx"-folder:

+verbose+
function obj = xxx(in)
  if nargin == 0
    fprintf('xxx: empty arguments\n');
    obj.str = 'no init';
  else
    fprintf('xxx: argument %d\n', in);
    obj.str = in;
  end
  obj = class(obj, 'xxx');
end % function
-verbose-

Using classdef:

+verbose+
classdef xxx2
 
  properties
    str
  end
 
  methods
    function obj = xxx2(in)
      if nargin == 0
        fprintf('xxx: empty arguments\n');
        obj.str = 'no init';
      else
        fprintf('xxx: argument %d\n', in);
        obj.str = in;
      end
    end
  end
 
end
-verbose-

Octave:

+verbose+

>> a(1,3) = xxx(3)

xxx: argument 3
a =

  <class xxx>

>> a.str

error: invalid index for class

>> b(1,3) = xxx2(3)

xxx: argument 3
xxx: empty arguments
b =

<object array xxx2>

>> b.str

ans = no init
ans = no init
ans =  3
-verbose-

MATLAB R2016b:

+verbose+

>> a(1,3) = xxx(3)

xxx: argument 3
xxx: empty arguments

a =

xxx object: 1-by-3

>> a.str

Access to an object's fields is only permitted within its methods.
 

>> b(1,3) = xxx2(3)

xxx: argument 3
xxx: empty arguments

b =

  1×3 xxx2 array with properties:

    str

>> b.str


ans =

no init


ans =

no init


ans =

     3
-verbose-

Therefore I declare this as "won't fix", as a future save workaround exists and close this item.

Kai Torben Ohlhus <siko1056>
Group Member
Thu 02 Mar 2017 03:57:18 PM UTC, comment #3: 

Addendum: I think it would be nice not to wait for the last info,
but to give this report a chance to receive a patch!!

Ernst Reissner <ernstreissner>
Thu 02 Mar 2017 03:56:10 PM UTC, comment #2: 

This is definitely not a wish but a real bug.
Matlab documentation clearly says,
that the constructor with no arguments is invoked
to initialize a(1),...a(6),
whereas to initialize a(7) the constructor given explicitly is invoked.

In contrast to this,
octave creates a kind of raw objects
without attributes
but with the correct class assigned.
This is not done via invocation of constructor,
but hardcoded.

The result for me, implementing an arithmetics
with class pn (polymorphic number) is,
that in Matlab I would create the 0 value with pn().
That way, a(1),...,a(6) are initialized implicitly with
the 0 value in pn.

This is to make it compatible with double,
where a(7)=4.3
makes a(1),...,a(6) be initialized with the double value 0.

Currently, octave initializes a(1),...a(6)
with some half finished objects
which carry no value at all, while being of the correct class.


As regards the question, whethe the constructor pn()
shall be invoked for a(1),...,a(n) separately,
or just once and then copied,
this is almost immaterial,
because if pn() has no side effects,
the result is the same.
Note that not the reference is copied, but the object.

It is just a matter of optimization,
that the constructor is invoked once only,
and the rest is just memory copy.

So, if I could vote, I would vote urgently,
to recover severity: it is a bug.

The result for me is,
that I very frequently have to check for the object
with all attributes uninitialized,
which is anoying. !!!!

Thank you for considering. !!!!

Ernst Reissner <ernstreissner>
Thu 02 Mar 2017 12:16:47 AM UTC, comment #1: 

Minor correction, the Matlab page referenced says

> MATLAB calls the SimpleValue constructor once and copies the returned object to each element of the array.


So the constructor is called once with an argument of 7, and once with no arguments.

It would be nice to see a test result in Matlab to confirm how many times the constructor method is actually called and verify the values in the object array, since Matlab help pages have been known to be unclear and vague. But something probably does need fixing here.

Mike Miller <mtmiller>
Group Member
Tue 21 Feb 2017 12:59:39 PM UTC, original submission:  

According to

https://de.mathworks.com/help/matlab/matlab_oop/initialize-object-arrays.html

if matlab is called with
a(7) = SimpleValue(7),
SimpleValue a class constructor,
then for initializing a(1), ...a(6),
the default constructor is invoked as SimpleValue().

I tried this with a very simple class
defined in folder @xxx
with the following constructor:


function obj = xxx(in)
  if nargin == 0
    printf("xxx: empty arguments");
    obj.str = "no init";
  else
    printf("xxx: argument %d ", in);
    obj.str = in;
  end
  class(obj, "xxx");
end % function


When assigning
a(3)=xxx(3),
then this constructor is invoked only once.
If I try a(3), then str is initialized,
whereas if I try a(2), it is not initialized.


This seems a bug.
In my case, I implement an arithmetics in java
which shall behave here as double,
i.e. the default constructor without arguments
shall return an object with 0 value,
but it returns an uninitialized object.


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

Digest:
   bug dependencies.

 

Carbon-Copy List
  • -email is unavailable- added by siko1056 (Posted a comment)
  • -email is unavailable- added by mtmiller (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 14 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-08-22 mtmiller Summarydefault constructor for old-style class objects is not called when initializing an array default constructor for legacy class objects is not called when initializing an array
    2017-08-22 mtmiller SummaryDefault constructor for objects in array default constructor for old-style class objects is not called when initializing an array
    2017-08-22 mtmiller Dependencies- bugs #51820 is dependent
    2017-03-03 mtmiller StatusWont Fix Confirmed
        Open/ClosedClosed Open
    2017-03-03 siko1056 Priority5 - Normal 3 - Low
        StatusNeed Info Wont Fix
        Open/ClosedOpen Closed
        Release4.2.0 dev
    2017-03-02 mtmiller CategoryNone Interpreter
        Severity3 - Normal 1 - Wish
        Item GroupNone Matlab Compatibility
        StatusNone Need Info
    2017-02-21 ernstreissner Carbon-Copy- Added -email is unavailable-

    Back to the top

    Powered by Savane 3.13-461b.
    Corresponding source code