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?
|
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.
|
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.
|
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. !!!!
|
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.
|
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:
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.
|