Borrar filtros
Borrar filtros

Default Class Property Value That Contains Class

3 visualizaciones (últimos 30 días)
David DeVries
David DeVries el 24 de Oct. de 2019
Comentada: David DeVries el 24 de Oct. de 2019
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.

Respuestas (1)

Steven Lord
Steven Lord el 24 de Oct. de 2019
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 comentario
David DeVries
David DeVries el 24 de Oct. de 2019
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Properties en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by