Initializing class property values outside of constructor

Here is a very simple class definition:
classdef DummyClass < handle
properties
myMap = containers.Map('KeyType','uint32','ValueType','uint32');
end
methods
end
end
Now, here is a very strange series of commands using this class:
>> d = DummyClass();
>> d.myMap(1) = 1;
>> clear;
>> d = DummyClass();
>> d
d =
DummyClass with properties:
myMap: [1x1 containers.Map]
>> d.myMap
ans =
Map with properties:
Count: 1
KeyType: uint32
ValueType: uint32
Why does the value of the myMap property persist across calls to clear !?!
I have noticed that when I move the initialization of the myMap property into the class constructor this does not happen. I already have several fixes for this 'problem' so I am not looking for a solution here - instead I am looking for an explanation.
Thank you.

回答 (2 件)

per isakson
per isakson 2014 年 2 月 19 日
編集済み: per isakson 2014 年 2 月 19 日

0 投票

Because Matlab behaves that way;-)
clear doesn't clear 'containers.Map', but clear classes does - according to inmem.
[~,~,C] = inmem; C
d = DummyClass();
clear
[~,~,C] = inmem; C
clear classes
[~,~,C] = inmem; C
returns
C =
'com.mathworks.mlwidgets.workspace.WhosInformation'
C =
'com.mathworks.mlwidgets.workspace.WhosInformation'
'containers.Map'
C =
Empty cell array: 0-by-1
.
Later
inmem
d = DummyClass();
d.myMap(1) = 117;
clear
d = DummyClass();
d
d.myMap(1)
returns
ans =
'workspacefunc'
'onCleanup'
'imformats'
d =
DummyClass with properties:
myMap: [1x1 containers.Map]
ans =
117
and
inmem
d = DummyClass();
d.myMap(1) = 117;
clear classes
d = DummyClass();
d
d.myMap(1)
returns
ans =
'workspacefunc'
'onCleanup'
'imformats'
d =
DummyClass with properties:
myMap: [0x1 containers.Map]
Error using containers.Map/subsref
The specified key is not present in this container.

3 件のコメント

KARL BEUTNER
KARL BEUTNER 2014 年 2 月 19 日
Your answer tells me that this behavior is not a bug in matlab, but can you explain more about WHY this is the behavior? In the second output of C it shows that containers.Map is inmem, but it does not show that DummyClass is inmem. If DummyClass is not inmem, I would have expected it to at least get a new value for myMap once it comes back into mem, instead of getting whatever value was already there.
Is there documentation you can recommend that explains more about the principles and designs that were implemented when coming-up with the behavior? When MATLAB behaves differently from the way I would have expected, I like to understand WHY.
per isakson
per isakson 2014 年 2 月 19 日
編集済み: per isakson 2014 年 2 月 19 日
Matlab is not open source and we cannot know WHY.
In the examples, which I added to my answer, clear does not behave as expected, i.e. as I interpret the documentation. However, clear classes does!
Whether this is an issue, a bug or a mistake in the documentation, I can only guess. (Or I have missed something in the documentation. That happens.)
KARL BEUTNER
KARL BEUTNER 2014 年 2 月 19 日
編集済み: KARL BEUTNER 2014 年 2 月 19 日
Just because Matlab is not open source does not mean I cannot know WHY something was implemented or designed in a certain way, it simply keeps me from knowing HOW it was implemented.
I don't care how Matlab has implemented its' classes, I simply want to know why they thought this would be a good feature to have. It seems to me that in this particular case, the MathWorks team may be trying to allow for something like a static class property but have failed badly.
Alternatively, this is truly a bug.

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

per isakson
per isakson 2014 年 2 月 19 日
編集済み: per isakson 2014 年 2 月 21 日

0 投票

myMap is the default map of the class DummyClass. The value of myMap is a containers.Map. That map is share among all instances of DummyClass, whether or not it holds data. That seems to be by Design.
>> d = DummyClass
d =
DummyClass with properties:
myMap: [0x1 containers.Map]
>> d.myMap(1)=117;
>> e = DummyClass
e =
DummyClass with properties:
myMap: [1x1 containers.Map]
>> e.myMap(1)
ans =
117
[...]Default Values
Classes can define default values for properties. A default value is defined by the class and each instance of the class is assigned that default value. Default values can be used when there is a constant default that can be documented with the class definition. Default values can save space in MAT-Files because defaults are saved once per class and values of properties not differing from default values don’t need to be saved. Generally speaking default values should be literal values wherever possible so that it is clear what the default is.
.
Later
[...] Note: Evaluation of property default values occurs only when the value is first needed, and only once when MATLAB first initializes the class. MATLAB does not reevaluate the expression each time you create a class instance.

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

質問済み:

2014 年 2 月 19 日

編集済み:

2014 年 2 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by