Default Class Property Value That Contains Class

4 次查看(过去 30 天)
In MATLAB 2019b, property validation was added to classes. I'm finding this is a good way to both easily validate property values as well as keep it clear in the code about what type each property is. I do run into problems when I have a property in a class that will contain an object of another class, and I want the property to contain an object or be empty. As a toy example, imagine two classes:
classdef Dog
properties
Name (1,1) string
FavouriteToy (1,1) DoggieToy = DoggieToy.empty
end
methods
function obj = Dog(Name, FavouriteToy)
arguments
Name (1,1) string
FavouriteToy (1,1) DoggieToy = DoggieToy.empty
end
obj.Name = Name;
obj.FavouriteToy = FavouriteToy;
end
end
end
classdef DoggieToy
properties
Name (1,1) string
IsChewy (1,1) logical
end
methods
function obj = DoggieToy(Name, IsChewy)
obj.Name = Name;
obj.IsChewy = IsChewy;
end
end
end
Within "Dog", I want the property "FavouriteToy" to be optional, e.g. the user can leave it empty if they so choose.
Leaving the classes as is triggers an error in Matlab that the "FavouriteToy" default value doesn't match the size constraint of (1,1). Is there any way to specify that a property (or argument to a function) can be either empty or of some size (in this case (1,1) )?
My current workaround is to adjust "Dog" to:
classdef Dog
properties
Name
FavouriteToy DoggieToy {Dog.mustBeScalarOrEmpty(FavouriteToy)} = DoggieToy.empty
end
methods
function obj = Dog(Name, FavouriteToy)
arguments
Name (1,1) string
FavouriteToy DoggieToy {Dog.mustBeScalarOrEmpty(FavouriteToy)} = DoggieToy.empty
end
obj.Name = Name;
obj.FavouriteToy = FavouriteToy;
end
end
methods (Static = true)
function mustBeScalarOrEmpty(var)
if ~(isscalar(var) || isempty(var))
error('Not scalar nor empty.');
end
end
end
end
This achieves my desired behaviour, but I'm thinking there must be a cleaner way. If not...feature request for 2020a? I feel that this is a pretty common use case for both class property and function argument validation.

回答(1 个)

Steven Lord
Steven Lord 2019-10-24
If a Dog has a FavouriteToy, must that FavouriteToy have a name? If so, could you represent the case where a Dog has no FavouriteToy by setting that DoggieToy's Name property to empty?
noToy = DoggieToy("", false);
That way the Dog's FavouriteToy property will always be scalar. Checking if a dog has a favorite toy would then require checking if the toy's Name has strlength greater than 0.
  1 个评论
David DeVries
David DeVries 2019-10-24
Thanks Steven!
An excellent suggestion, though it feels like a work around to me. It also seems that we would basically be embedding an "is this object empty" flag within objects, whereas using Matlab's provided empty object functionality would be preferrable and lead to less possible future confusion.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by