Date: May 21, 2013 5:50 PM
Author: Steven Lord
Subject: Re: Value classes - do you use them? also syntax questions

"Eric Sampson" <ericDOTsampson@gmail.com> wrote in message

news:kngl1l$gd1$1@newscl01ah.mathworks.com...

> I posted the below on Answers but got no responses, perhaps the newsgroup

> is a better forum to discuss this :)

> ---------

>

> So I can't really recall ever creating a value class, all mine seem to be

> handle classes. Is that the same as your experience? Unless I'm completely

> missing something, using value classes seems to be a bit ungainly, because

> you always have to do two steps like this:

>

> foo = MyValueClass(5); % sets a property 'val'

> foo = foo.double();

> plot(foo.val);

Why does the DOUBLE method of the MyValueClass return an object instead of a

double array?

http://www.mathworks.com/help/matlab/matlab_oop/converting-objects-to-another-class.html

> With a handle class, I'd just do this:

>

> foo = MyHandleClass(5);

> plot(foo.double()); % double returns the new obj.val directly.

>

> Am I completely out to lunch with this understanding of value classes?

I suggest you read through this if you haven't already.

http://www.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html

My mental model: if I want to use the object like a MATLAB array, I make it

a value.

A = [1 2 3; 4 5 6];

C = A;

b = sin(A);

If the object is a "pointer" or refers to something with physical meaning

(where it doesn't make sense to "copy" it by doing assignment) I make it a

handle.

fid = myFileObject('c:\temp\temp.txt'); % There is only one temp.txt file in

c:\temp

h = myCompositeGraphicsObject; % h is associated with an object on screen

h2 = h % h2 and h both refer to the same object; there's no copying being

done here

> As an aside, sometimes it seems like it would be nice to have the above

> syntax of a handle class, but be able to make independent copies of a

> given object like a value class... Anyone else think so?

That's what matlab.mixin.Copyable is for.

http://www.mathworks.com/help/matlab/ref/matlab.mixin.copyableclass.html

--

Steve Lord

slord@mathworks.com

To contact Technical Support use the Contact Us link on

http://www.mathworks.com