how to create an object with "handle like" properties and "value like" assignment

12 views (last 30 days)
I want to create an object that has the following "properties":
  • Copy-on-assignment*
  • The ability to update properties without returning a new object in object methods
Ex:
a = myObj;
a.setx(1); % a.x should now equal 1 (x property updated without reassignment of a)
b = a; % Copy-on-assignment (changes to a do not affect b and vice-versa)
b.incrementx(); % b.x should now equal 2 (x property updated without reassignment of b)
% at this point a.x should equal 1 and b.x should equal 2
a.x % returns 1
b.x % returns 2
My current approach is to create 2 objects, one being a value object (myObj) and the other being a handle object (helperObj). The value object (myObj) has a protected property (SetAccess = private) of type handle object (helperObj) and any methods that need to update values can do so in the handle object (helperObj).
classdef myObj_internal < handle
properties
x
end
end
classdef myObj
properties
otherprops
end
properties(SetAccess = private)
internal@myObj_internal
end
methods
function obj = myObj()
obj.internal = myObj_internal;
end
function [] = setx(obj,x)
obj.internal.x = x;
end
function [] = incrementx(obj)
obj.internal.x = obj.internal.x + 1;
end
end
end
Ex:
a = myObj;
a.setx(1);
a.otherprops = 3;
b = a;
b.incrementx();
b.otherprops = 4;
a.internal.x % is 2, should be 1
b.internal.x % is 2, should be 2
a.otherprops % is 3, should be 3
b.otherprops % is 4, should be 4
This almost achieves what I am after because it allows for a cache that is only accessible to methods of myObj and copies other properties (otherprops) but fails to keep caches seperate. If all else fails, I can make myObj extend handle and implement a copy method but I am trying to avoid this. I want to make my api accessable to novice/average users and eliminate potential pitfalls.
Any suggestions would be appreciated
*I know matlab creates a shared data copy and is copy-on-write

Answers (1)

Guillaume
Guillaume on 14 May 2019
That is simply not possible in matlab.
"I want to make my api accessable to novice/average users and eliminate potential pitfalls".
I'd say that trying to implement concepts that may be common in other languages but completely antithetical to matlab design is not going to help. Yes, when I started writing classes in matlab (after years of C++ and C#) finding that value classes in matlab couldn't have internal state was disconcerting. Now, I'm used to it and I wouldn't dream on trying to hack around it because that's a sure way to confuse novices.
If you want internal state, use a handle class. to help with copy derive from matlab.mixin.Copyable. Users will have to use your class, the same way they have to use graphic handles, and they'll work the same.

Categories

Find more on App Building in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!