Class 'dependent' property warning

Hi,
I'm trying to build a class, one of the properties of which is also a class. I want to be able to index the properties of the contained class from the top class. This toy example shows what I mean I hope... I make the inner class and override it's disp method so it displays as I see it....
<...okay this thing won't let me paste code into this so I can't actually ask my question. This editor is really not working very well for me at least...>

11 Comments

While there are indeed some issues with the editor, pasting into it is not a known problem.
What happens when you paste?
Arwel
Arwel on 8 Mar 2019
Edited: per isakson on 10 Mar 2019
Hi,
I'm on OSX, and I select my code in the matlab editor, and copy with cmd-c. Then just to check it's copied properly, I tried to see if it pastes correctly into MSWord (it does, with the colour formatting - see attached screenshot), so I know that is top of my clipboard. But if I then do cmd-v into here I get,,,
..a URL about queuing theory (maybe apt!) from days ago tha I had copied previously. I'm on OSX High Sierra, R2016b and Chrome as a browser. I cannot paste code from the editor, nor find a workaround. Cmd-V into MSWord gives the code, and without doing any other copy, cmd-V into this gives an old URL.
Perhaps try a different browser (Safari?) although I can copy and paste code from my version of MATLAB (on High Sierra) into this site using Chrome...
I'll point the relevant people to this thread so they can investigate the copy/paste issue.
In the meantime, even if it's not ideal, we can always work with screenshots.
Adam
Adam on 8 Mar 2019
Edited: Adam on 8 Mar 2019
Copy-paste issues aside I'm not really understanding what the question is or how the code in that screenshot relates. You take about a class that contains a property which is an object of another class, but your example code just shows one class with basic numeric properties.
In theory, indexing into properties of an object that is itself the property of a class should behave as you would expect it to. What exactly is the issue? Your question title talks about a Dependent property warning, but you don't mention this in the question body or give the exact warning you get.
(If you can't post actual code though please at least post a screenshot of Matlab editor rather than Word. Word's underlining of things in red and green just adds confusion while any Matlab underlining that there may be is potentially useful.)
Adam, no i haven't actually asked the question yet!
So rather than screenshots I'm just going to re-type my toy example.....
I have a class, one parameter of which is a sub-class, and I want to reference the sub class with easy 'dot' referencing from the top class, and to do some custom formatting when disp is excecuted on the top class. Everything is actually working in exactly the way I want, so my question is just about the warning that's generated and can I ignore it.
So defining as follows...
classdef biggerClass
properties
name
sClass = smallclass;
val1 = @obj.sClass;
end
methods
function obj = set.name(obj,val)
obj.name = val;
end
function obj = set.val1(obj,val)
obj.sClass.myVal1 = val;
end
function obj = disp(obj,val)
fprintf('name: %s \n', obj.name);
disp(obj.sClass);
end
end
end
Then....
classdef smallclass
properties
myVal1 = 1;
myVal2 = 2;
end
methods
function obj = set.myVal1(obj,val)
obj.myVal1 = val;
end
function obj = set.myVal2(obj,val)
obj.myVal2 = val;
end
function obj = disp(obj,val)
fprintf('Val 1 : %d \n',obj.myVal1);
fprintf('Val 2 : %d \n',obj.myVal2);
end
end
end
Now, this gives exactly the behaviour I want. If I go...
>> a = biggerClass
..I get
a =
name:
Val 1: 1
Val 2: 1
...and I can go...
>> a.val1 = 10
a =
name:
Val 1: 10
Val 2: 1
..and this behaviour is exactly what I want, so I'm happy. The only concern is that editor is highlighting the 'obj.sClass.myVal1 = val' line with a warning "A set method for non-dependant property should not access another property". So my question is basically before I scale this up and use this (working) approach in the real thing, why this warning, can I ignore it and if not what should I change?
In these situations I always go for the double-property approach in the link Steven Lord pointed to with a dependent property with a set function that stores its data in a private property. It's extra coding, but I don't like ignoring warnings in general. Even if I think I'll never use the class in a way that would cause the problems it refers to I'd just rather avoid even the chance of such hassle down the line, especially when I am working with a team and other people may be less familiar with the potential problems than I am.
I don't really understand the need for BiggerClass.Val1. Why can't it be accessed directly from BiggerClass.Sclass since it's just a duplicate. And if it's really needed, it should indeed be a dependent property.
I agree about warnings, which is why I'm asking really. I'm not familliar with the Stephen Lord link you're referring to though... can you post it?
Scroll to the end of my Answer below. I linked to a documentation page calling out one potential issue that Code Analyzer warning is trying to guard against.
With regards to copy/paste:
MacOS High Sierra, recent Firefox: I cannot paste into the main portions of any Question, Answer, or Comment that I am editing. I also cannot copy out of anything I am editing. I can copy out of saved text -- so if I want to check the code of something I am writing, I can tell it to save the response and then copy out of the saved version.
My work-around has been to use Safari.

Sign in to comment.

Answers (1)

I believe one of the reasons (maybe the main reason) for that Code Analyzer warning is to avoid accidentally getting into a recursive situation. Consider:
classdef classWithRecursionProblem
properties
x = 0;
y = 0;
end
methods
function obj = set.x(obj, newx)
obj.x = newx;
obj.y = obj.y + 1;
end
function obj = set.y(obj, newy)
obj.y = newy;
obj.x = obj.x + 1;
end
end
end
Construct an instance of this object then try to update one of its properties.
q = classWithRecursionProblem
q.x = 1
You should receive an error indicating that there's a problem with recursion. The set method for the x property causes the set method for the y property to be called and the set method for the y property causes the set method for the x property to be called. It's like a pair of dictionary definitions: "Loop, infinite: see Infinite loop" and "Infinite loop: see Loop, infinite".
Note also that this is a Code Analyzer warning not a Code Analyzer error. As long as you avoid trying to assign to another property inside a different property's set method you're probably okay. Actually, reading the Details for that Code Analyzer warning they describe another somewhat related scenario related to loading objects, linking to this documentation page that provides more information.

Categories

Tags

Asked:

on 8 Mar 2019

Edited:

on 10 Mar 2019

Community Treasure Hunt

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

Start Hunting!