Class 'dependent' property warning

2 ビュー (過去 30 日間)
Arwel
Arwel 2019 年 3 月 8 日
編集済み: per isakson 2019 年 3 月 10 日
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 件のコメント
Steven Lord
Steven Lord 2019 年 3 月 8 日
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.
Walter Roberson
Walter Roberson 2019 年 3 月 8 日
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.

サインインしてコメントする。

回答 (1 件)

Steven Lord
Steven Lord 2019 年 3 月 8 日
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.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by