Bizarre value class behavior

Below is a self-explanatory VALUE class I developed for purposes of illustration.
classdef ClassA
properties
x;
y;
end
methods
function obj = ClassA()
obj.x = 0;
obj.y = 0;
end
function setX(obj, val)
obj.x = val;
end
function setY(obj, val)
obj.y = val;
end
function obj = set.x(obj, val)
*obj* .x = val;
end
function obj = set.y(obj, val)
obj.y = val;
end
end
end
I instantiated it as follows:
a = ClassA();
Good. That worked. Now to test the setter methods:
a.x = 1;
a.y = 2;
Those worked too. But doing this:
a.setX(3)
a.setY(5)
causes the prompt to not echo the value of the properties as is done when the setter methods are used. Why? And when I enter the variable name in the prompt, the property values are echoed back but they do not change upon using setX and setY. This is puzzling. I followed proper VALUE class syntax for setX and setY. Help please!

 Accepted Answer

per isakson
per isakson on 31 Aug 2014
Edited: per isakson on 2 Sep 2014
"I followed proper VALUE class syntax for setX and setY." &nbsp No, that's handle class syntax. With a value class you create a new instance, which you need to return.
With the class definition modified
>> a = ClassA
a =
ClassA with properties:
x: 0
y: 0
>> a = a.setX( 17)
a =
ClassA with properties:
x: 17
y: 0
or
>> a = setY( a,117)
a =
ClassA with properties:
x: 17
y: 117
where
classdef ClassA
properties
x;
y;
end
methods
function obj = ClassA()
obj.x = 0;
obj.y = 0;
end
function obj = setX(obj, val)
obj.x = val;
end
function obj = setY(obj, val)
obj.y = val;
end
function obj = set.x(obj, val)
obj.x = val;
end
function obj = set.y(obj, val)
obj.y = val;
end
end
end

1 Comment

That is right. Thank you per isakson and Image Analyst.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 31 Aug 2014
They're methods. Not all methods and functions echo stuff to the command window. Some do, for example jet, colormap, etc. Some do not, for example "hold on", "axis off", etc. Note that, while your functions setX() and setY() do something, they don't actually have any return arguments defined . So they won't even put anything into "ans" or return anything whatsoever. Hence nothing will be echoed to the command window, like if they returned an output argument.

5 Comments

John
John on 31 Aug 2014
Edited: John on 31 Aug 2014
Thank you for the insight, Image Analyst . Now, I am still puzzled over why setX and setY do not set properties x and y.
It's been a while since I used a class. I'm surprised that your last two methods even work since they have dots in the method name. I see the setX and setY methods take two input arguments while you're passing only 1 - is that right (I'd have to review)? And did you actually check after you called to see if they were set by putting these lines
% Report values to command window
a.x % Let's see if they actually changed.
a.y
John
John on 31 Aug 2014
Edited: John on 31 Aug 2014
The functions set.x and set.y are just setter methods and that is the official MATLAB syntax for setters, a new MATLAB OOP feature inspired by C# and Java.
Anyway,
I changed setX and setY to:
function setX(obj, val)
obj.x = val;
disp(obj.x)
end
function setY(obj, val)
obj.y = val;
disp(obj.y)
end
to display the values of the current object's x and y properties before leaving the function and it does echo the updated value (no surprise there). But when I enter the object name on the command prompt, a , the values of x and y are not updated, which is what puzzles me. It is as if the variable name a that refers to the instance of ClassA I am working with always points to a copy of the constructed variable and is immutable.
I tried restarting MATLAB -something which has cleared up quirks related to changing the source for the class- and repeated my tests but I get the same confusing behavior.
Please help :-)
Read my answer
John
John on 31 Aug 2014
Edited: John on 31 Aug 2014
Thank you per isakson

Sign in to comment.

So does this mean that for a value class, if I want to call a function on an instance of that value class which modifies the state of the instance and also returns data from the instance, I would have 2 return values: 1) the modified instance and 2) the data value returned
Example: A value class that implements a bag of items.
%instantiate a bag class and add the number 1 into it
bag = Bag();
bag.add(1);
%Now remove the item from the bag
[bag, returnedItem] = bag.remove();
Notice that the state of 'bag' is modified after the remove action and has to be reassigned to the variable 'bag'. If the remove action were called like this:
returnedItem = bag.remove();
, the variable bag would still refer to the unmodified bag value with item '1' still in it even though '1' is returned.
Am I right?
I think that's a bit cumbersome but manageable.

Categories

Products

Asked:

on 31 Aug 2014

Edited:

on 2 Sep 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!