Object-Oriented: strange behavior that leads to shared reference
Hi, is my understanding of Matlab OO incorrect or something else is going on here ?
I cannot explain why this code assert :
Given this simple handle class :
classdef TestA < handle
properties
Name string = "default value for TestA";
end
end
An using it like this ( see the explicit size of 1,1 )
classdef TestB < handle
properties
PropA (1,1) TestA % please note the (1,1) size
end
end
An this test usage :
b1 = TestB;
b1.PropA.Name = "in b1 object";
b2 = TestB;
b2.PropA.Name = "in b2 object";
assert(b1.PropA ~= b2.PropA)
This assertion failling clearly state that the PropA property of both objects b1 and b2 is the same object.
Is this a documented behaviour ?
I was expecting to get a new TestA object in every TestB object.
note that we get the same strange behavior with this syntax:
classdef TestB2 < handle
properties
PropA (1,:) TestA = TestA % please note the (1,:) size
end
end
so you get the strange behavior and you loose the (1,1) size constraint.
This can be a workaround, but again, far from clean and still no size constraint
classdef TestB3 < handle
properties
PropA (1,:) TestA = TestA.empty() % please note the (1,:) size
end
methods
function obj = TestB3()
obj.PropA = TestA(); % explicitly create the object works
end
end
end
I can get the size constraint by using a Dependent property but this is so much code for something that basic…
Should i report a bug? or is it explained somewhere in the documentation ?Hi, is my understanding of Matlab OO incorrect or something else is going on here ?
I cannot explain why this code assert :
Given this simple handle class :
classdef TestA < handle
properties
Name string = "default value for TestA";
end
end
An using it like this ( see the explicit size of 1,1 )
classdef TestB < handle
properties
PropA (1,1) TestA % please note the (1,1) size
end
end
An this test usage :
b1 = TestB;
b1.PropA.Name = "in b1 object";
b2 = TestB;
b2.PropA.Name = "in b2 object";
assert(b1.PropA ~= b2.PropA)
This assertion failling clearly state that the PropA property of both objects b1 and b2 is the same object.
Is this a documented behaviour ?
I was expecting to get a new TestA object in every TestB object.
note that we get the same strange behavior with this syntax:
classdef TestB2 < handle
properties
PropA (1,:) TestA = TestA % please note the (1,:) size
end
end
so you get the strange behavior and you loose the (1,1) size constraint.
This can be a workaround, but again, far from clean and still no size constraint
classdef TestB3 < handle
properties
PropA (1,:) TestA = TestA.empty() % please note the (1,:) size
end
methods
function obj = TestB3()
obj.PropA = TestA(); % explicitly create the object works
end
end
end
I can get the size constraint by using a Dependent property but this is so much code for something that basic…
Should i report a bug? or is it explained somewhere in the documentation ? Hi, is my understanding of Matlab OO incorrect or something else is going on here ?
I cannot explain why this code assert :
Given this simple handle class :
classdef TestA < handle
properties
Name string = "default value for TestA";
end
end
An using it like this ( see the explicit size of 1,1 )
classdef TestB < handle
properties
PropA (1,1) TestA % please note the (1,1) size
end
end
An this test usage :
b1 = TestB;
b1.PropA.Name = "in b1 object";
b2 = TestB;
b2.PropA.Name = "in b2 object";
assert(b1.PropA ~= b2.PropA)
This assertion failling clearly state that the PropA property of both objects b1 and b2 is the same object.
Is this a documented behaviour ?
I was expecting to get a new TestA object in every TestB object.
note that we get the same strange behavior with this syntax:
classdef TestB2 < handle
properties
PropA (1,:) TestA = TestA % please note the (1,:) size
end
end
so you get the strange behavior and you loose the (1,1) size constraint.
This can be a workaround, but again, far from clean and still no size constraint
classdef TestB3 < handle
properties
PropA (1,:) TestA = TestA.empty() % please note the (1,:) size
end
methods
function obj = TestB3()
obj.PropA = TestA(); % explicitly create the object works
end
end
end
I can get the size constraint by using a Dependent property but this is so much code for something that basic…
Should i report a bug? or is it explained somewhere in the documentation ? object-oriented MATLAB Answers — New Questions