Custom class properties and method attributes

Is there a way to add custom property or method attributes to handle classes? It is done by several classes or toolboxes, e.g. the unit testing framework allows to add tags to the methods: Tag Unit Tests - MATLAB & Simulink (mathworks.com)
classdef ExampleTagTest < matlab.unittest.TestCase
methods (Test)
function testA (testCase)
% test code
end
end
methods (Test, TestTags = {'Unit'})
function testB (testCase)
% test code
end
function testC (testCase)
% test code
end
end
I'm talking about the attributes "Test" and "TestTags".
My application would greatly benetfit from a finer granular control of properties for a DTO (data transfer object) class. I want to use argument validation but add additional options like overridable properties and don't want to use inputParser anymore :)
Thanks!

댓글 수: 6

Matt J
Matt J 2021년 10월 26일
and don't want to use inputParser anymore
Why not? It's more flexible than argument validation.
Slower, bulkier, no auto completion, older... The new argument validations are awesome.
Matt J
Matt J 2021년 10월 26일
They're convenient when all the validation can be done inline, but for what you describe, it sounds like you're going to have to write your own custom validation function anyway.
You're right - a custom validation function will be used. But I'd nevertheless like to use the property validation combined with a separate validation function.
Reasons are simple, arguments get casted to expected data types and early errors are thrown when mandatory (non-default) properties are missing (to be set by the constructor). I can see, what properties (a.k.a input parameters) are available and I can use meta class/property data to iterate through all of them etc.
Plus, I have awesome auto completion proposals, via
function obj = SensorYieldAnalysisHelper(PropertyArgs)
%SensorYieldAnalysisHelper Construct an instance of this class
% Detailed explanation goes here
arguments
PropertyArgs.?SensorYieldAnalysisHelper % support all public properties AND automatic parameter completion!
end
end
I mean, how cool is that?
Does anything above work with inputParser (honest question!)?
As far as I know Matlab does not (version<=R2023) support custom property attributes. But if you are not using some of the existing ones for anything you might use them. For example SetObservable or GetObservable can be used to mean something else if you are not using listeners.
Thanks for that recommendation but I'm quite reluctant to abuse existing attributes that have a pretty clear meaning :)

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt J
Matt J 2021년 10월 26일
I don't know why it's not documented, but apparently there is a Description attribute that can be added, e.g.,
classdef myclass
properties (Description='propTag')
prop
end
methods (Description='methTag')
function meth1
end
end
methods
function meth2
end
end
end

댓글 수: 3

Jan Kappen
Jan Kappen 2021년 10월 26일
편집: Jan Kappen 2021년 10월 26일
Wow!
This is awesome, how do you know that?
Would you expect that Description field to be existent in the next releases?
mm =
class with properties:
Name: 'myclass'
Description: ''
DetailedDescription: ''
Hidden: 0
Sealed: 0
Abstract: 0
Enumeration: 0
ConstructOnLoad: 0
HandleCompatible: 1
InferiorClasses: {0×1 cell}
ContainingPackage: [0×0 meta.package]
RestrictsSubclassing: 0
PropertyList: [1×1 meta.property]
MethodList: [26×1 meta.method]
EventList: [1×1 meta.event]
EnumerationMemberList: [0×1 meta.EnumeratedValue]
SuperclassList: [1×1 meta.class]
>> mm.PropertyList
ans =
property with properties:
Name: 'prop'
Description: 'propTag'
DetailedDescription: ''
GetAccess: 'public'
SetAccess: 'public'
Dependent: 0
Constant: 0
Abstract: 0
Transient: 0
Hidden: 0
GetObservable: 0
SetObservable: 0
AbortSet: 0
NonCopyable: 0
PartialMatchPriority: 1
GetMethod: []
SetMethod: []
HasDefault: 0
Validation: [0×0 meta.Validation]
DefiningClass: [1×1 meta.class]
Matt J
Matt J 2021년 10월 26일
편집: Matt J 2021년 10월 26일
Would you expect that Description field to be existent in the next releases?
Since it's not documented, it's hard to know for certain. Hopefully, someone from the MathWorks will weigh in.
Does this address your question, though? If so, I'd be much obliged if you would Accept-click the answer.
The problem is that Description and DetailedDescription come from the comment block before the property declaration. If you set Description from the property declaration, it will override the comments, but I'm not sure whether we can rely on that behavior.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Class Introspection and Metadata에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2021년 10월 26일

댓글:

2023년 6월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by