Clear Filters
Clear Filters

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

6 views (last 30 days)
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!

Answers (1)

Karan Singh
Karan Singh on 22 Aug 2023
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 Comment
Jan Kappen
Jan Kappen on 22 Aug 2023
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?

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!