How to set a default value to a map container in argument validation?
14 次查看(过去 30 天)
显示 更早的评论
I'm validating a bunch of arguments in class properties. Seems quite straightfoward to set a deatault for double, integer and string datatypes.
classdef newClass < handle
properties
someStr (1,:) string = "myStr"
someNum (1,1) double = 3.1415
end
But what is the right way to do this for a map container? Because when I add this line for a map container:
someDict containers.Map
I see the following warning:
0 个评论
采纳的回答
Steven Lord
2023-3-30
移动:Walter Roberson
2023-4-4
Because each function call is independent I'd expect you wouldn't have the same issue with a handle object in a function's arguments block that you would with a class's properties block. Indeed, this little experiment supports my expectation. If the default value for dict was only created once the second call to the function should have listed both oldMissingValue and myKey as keys of the dict variable.
listOfKeys = example1936099_arg(42, "myKey")
listOfKeys = example1936099_arg(-999, "oldMissingValue")
listOfKeys = example1936099_arg()
function listOfKeys = example1936099_arg(value, name, dict)
arguments
value (1, 1) double = pi;
name (1, :) string = "myStr";
dict containers.Map = containers.Map;
end
dict(name) = value;
listOfKeys = keys(dict);
end
更多回答(2 个)
Walter Roberson
2023-3-27
test = containers.Map
isa(test, 'handle')
Therefore if you were to pass in a container.Map to the constructor and copy what was received into a property, then the container would end up being shared with any other uses of the container.
To get around this, to get a private container.Map, you need to create the container.Map in the constructor (and make the property private and do not create any methods that can allow people to get at the container.Map). Or you could somehow clone the incoming container.Map (the class does not provide a method for copying / cloning though.)
If you want the container.Map to be shared then mark the property as Constant .
6 个评论
Steven Lord
2023-3-27
Consider the two classes attached to this answer. The class class1936099_A defines someDict to contain a containers.Map in the properties section of the class definition. The class class1936099_B assigns someDict a containers.Map in the constructor.
obj1 = class1936099_A;
obj2 = class1936099_A;
Neither obj1 nor obj2 have 'a' as a key in their someDict property.
[isKey(obj1.someDict, 'a'), isKey(obj2.someDict, 'a')]
Let's associate 'a' with 42 in obj1's property. What does obj2's property associate 'a' with now?
obj1.someDict('a') = 42;
obj2.someDict('a')
Let's try the same with class1936099_B.
obj3 = class1936099_B;
obj4 = class1936099_B;
[isKey(obj3.someDict, 'a'), isKey(obj4.someDict, 'a')]
obj3.someDict('a') = 42;
obj4.someDict('a')
obj1 and obj2 have handles to the same containers.Map object. obj3 and obj4 have handles to different objects.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!