Function argument validation and editor auto-completion in wrapper functions/classes

14 次查看(过去 30 天)
TLDR;
Is it possible to automatically use the same argument validation + auto completion in wrapper functions as the called function, i.e. functions or method that just call an underlying function with the same arguments, using argument validation from the underlying function?
Longer version:
Suppose I have two classes, MeasurementDatastore and FileImporter.
FileImporter implements various ways to load data. Some key methods (which are not defined in an abstract interface, yet, but could be) are:
classdef FileImporter < handle
properties (Dependent)
ImportFcn function_handle = @(in)myFileImportFunction(in)
end
properties
FilePaths (1,:) string {mustBeFile}
end
methods
function out = loadData(obj)
out = obj.ImportFcn();
% throw error if data is empty etc
end
function addDataFiles(obj, filenames)
arguments
obj
filenames (1,:) string {mustBeFile}
end
obj.FilePaths = [obj.FilePaths, filenames];
end
function addDataFolder(obj, folders, opts)
arguments
obj
folders (1,:) string {mustBeFolder}
opts.Recursive logical = true;
opts.Wildcard string
end
for folder = folders
... % search folders and files
obj.addDataFiles(files);
end
end
end
end
I.e., the class is used to first add folders or files and then load them using some custom importer fcn handle.
That class is now used in a higher level class MeasurementDatastore (quite simplificated example):
classdef MeasurementDatastore < handle
properties
LoadedData
Loader FileLoader % should be an interface/abstract loader
end
methods
function obj = MeasurementDatastore()
obj.Loader = FileImporter() % instantiate FileImporter class
end
% just a wrapper method to the underlying FileImporter class
function addDataFolder(obj, folders, opts)
arguments
% how to get the same argument validation + auto completion
% as in FileImporter?
end
obj.Loader.addDataFolder(folders, opts)
end
function loadData(obj)
obj.LoadedData = obj.Loader.loadData() % calls FileImporter class
end
end
end
Of course, the MeasurementDatastore does more, transforms the data etc, that's why I need this additional layer.
The big question is: How can addDataFolder automatically use the same argument validations the referenced class FileImporter already has defined? Do I have to copy the block? I don't like that due to maintenance.
Can a meta class be used somehow?
I know, one can automatically get all properties auto completed and even validated against the property validation via
function obj = MeasDataHelper(propertyArgs)
arguments
propertyArgs.?MeasDataHelper
end
end
which is pretty awesome. Is there a similar technique that can be used for simple wrapper/"forward" functions & methods?
Thanks!

回答(1 个)

Karan Singh
Karan Singh 2023-8-22
Hi Jan Kappen, it seems there is no other way than to just copy and paste the conditions for the required for argument validation and auto-completion. To achieve the same argument validation and auto-completion in the "addDataFolder" method of the "MeasurementDatastore" class as in the "FileImporter" class. We could modify the code as
function addDataFolder(obj, folders, opts)
arguments
obj
folders (1,:) string {mustBeFolder}
opts.Recursive logical = true;
opts.Wildcard string
end
obj.Loader.addDataFolder(folders, opts);
end
In this modified code, the "addDataFolder" method in the "MeasurementDatastore" class includes the same argument validation as in the "FileImporter" class. By specifying the validation attributes {mustBeFolder}, {mustBeFile}, etc., you ensure that the input arguments meet the required criteria.
  1 个评论
Jan Kappen
Jan Kappen 2023-8-22
yes this is the obvious solution, I was hoping for something more automated. Are there other patterns to pass arguments a couple of methods down the class hierachy without lots of copy & paste?

请先登录,再进行评论。

类别

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

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by