Why is it necessary/useful to have a HandleCompatible attribute?
Show older comments
Setting HandleCompatible=true in a value class doesn't seem to change its behavior in any way, other than to allow it to "mate" with handle classes to form subclasses. What purpose therefore does it serve to have a HandleCompatible attribute at all? Why not let ordinary value classes mate freely with handle classes?
Answers (2)
Riya
on 6 Sep 2023
2 votes
Hello Matt J,
As per my understanding, you want to know the purpose of using ‘HandleCompatible’ attribute.
Please note that, the `HandleCompatible` attribute in MATLAB value classes serves a specific purpose related to the memory management and behavior of objects. By default, value classes in MATLAB are designed to be lightweight and operate on a copy-by-value principle. This means that when you assign a value class object to a new variable or pass it as an argument to a function, a new copy of the object is created.
On the other hand, handle classes in MATLAB are designed to be reference-based and operate on a copy-by-reference principle. When you assign a handle class object to a new variable or pass it as an argument to a function, the new variable or function argument points to the same underlying object in memory.
The `HandleCompatible` attribute allows you to create value classes that behave like handle classes, enabling them to be passed by reference rather than by value. This can be useful in scenarios where you want to avoid the overhead of copying large objects or when you want multiple variables or functions to refer to the same object.
However, it is important to note that allowing ordinary value classes to mate freely with handle classes could lead to unexpected behavior and potential issues with memory management. Value classes are designed to be lightweight and immutable, whereas handle classes have different memory management considerations and can be modified. Mixing the behavior of value classes and handle classes without proper restrictions could result in unintended consequences and make the code harder to reason about.
By providing the `HandleCompatible` attribute, MATLAB allows you to explicitly choose which classes should behave like handle classes, providing a clear distinction between value and handle semantics. This helps maintain the expected behavior of value classes while still allowing you to create reference-based objects when needed.
You can also refer the following documentation to know more about 'HandleClasses':
- https://www.mathworks.com/help/matlab/matlab_oop/supporting-both-handle-and-value-subclasses-handlecompatible.html?searchHighlight=handlecompatible%20%20attribute&s_tid=srchtitle_support_results_2_handlecompatible%2520%2520attribute
- https://www.mathworks.com/matlabcentral/answers/631669-are-handle-classes-the-fastest-containers?s_tid=srchtitle
- https://www.mathworks.com/matlabcentral/answers/2013932-what-exactly-are-handle-compatible-classes-are-they-a-combination-of-handle-and-non-handle-classes?s_tid=srchtitle
I hope it helps!
10 Comments
"This means that when you assign a value class object to a new variable or pass it as an argument to a function, a new copy of the object is created."
Is this really true for passing a value class object to a function in all cases? I thought that Matlab treats all inputs as pass by reference (i.e., no copy is made) unless the function internally modifies an input.
If this were true, then, for example, passing large numeric arrays into functions would be very inefficient.
However, upon reading some documentation, I'm now not so sure about what happens when passing a value object to a function that does not modify the input.
Bruno Luong
on 7 Sep 2023
@Paul "a new copy" means a new copy of the meta data, not the data itselft that can be shared.
Note that in the recent MATLAB it is not clear how a copy-on-write behavior where you can read here and there is no longer a strict internal mechanism, I'm unable to understand the mechanisme but something more complex is going on for sure.
Paul
on 7 Sep 2023
What is the "meta data"?
From this link: "When you pass a value object to a function, MATLAB creates a copy of that object in the function workspace."
When I read that, I read it to mean a copy of the entire object. However, that clause is under a section called "Modifying Value Objects in Functions", so maybe that should really say: ""When you pass a value object to a function *that modifies that modifies the object" (or something like that).
Bruno Luong
on 7 Sep 2023
Edited: Bruno Luong
on 7 Sep 2023
The doc is not supposes to describe the plombing behind the scene, MATLAB meta data (mxArray structure) is the plombing.
The handle object data is a hashcode to the real unique class data corresponds to this hashcode (stored somewhere else). So even if handle object is duplicated, their hashcode is identical and correspond to the same object.
A non-handle object is the object with a specific copy of the data.
Note that two non-handle objects one copy of other
foo = bar;
can share internally by the same data if no modification of the content occurs. When a modification is requested then their content are duplicated then modified (copy-on-write). This is a plumbing that not documented. You won't find this in ANY official documentation.
Bruno Luong
on 7 Sep 2023
May be when you want to declare a subclass of an non-handles class + behave like a handle?
Matt J
on 7 Sep 2023
Bruno Luong
on 7 Sep 2023
May be to declare a class that have a copy behavior like handle class buth without all the other stuffs of handle that you don't need it?
Bruno Luong
on 7 Sep 2023
Shouldn't be since a trillion graphic handles working in various graphical display would be catastrophic.
Maybe because in some cases you may want a class to have copy-by-reference semantics but you want its subclasses to have regular copy-by-value semantics? The HandleCompatible attribute of a value class is not automatically inherited by subclasses, whereas all subclasses of handle must have copy-by-reference semantics.
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!