Function argument validation and editor auto-completion in wrapper functions/classes dependent on function handle argument validation
조회 수: 3 (최근 30일)
이전 댓글 표시
Introduction:
Let's assume I have a abstract class "Device". I now have multiple implementations of the "Device" class. For instance one of the implementations is called "SerialDevice", another implemenation is called "TCPIPDevice". The constructors of those childclasses might take different arguments.
Additionally, I wrote a wrapper "DeviceWrapper" class with a constructor that has two (constructor) function signatures. If the first argument is a function handle to a constructor of a child class of "Device" I want to call the constructor of that class and pass on all additonal arguments provided to the "DeviceWrapper" class to the class constructor.
Problem: I now want to provide the appropriate autocompletion / code suggestion for the constructor arguments to the constructor the user has selected by the first argument. I haven't found any option to dynamically generate those depended on the first argument (function handle to the child class constructor of "Device")
Motivation: I often encouter this issue when I implement more complex interfaces that includes wrapper funtions that pass on arguments to other functions.
Question 1: How can I manage to provide full autocompletion for any constructor function handle that is provided as a first argument (without specifying it for every function handle separately, even though I don't knwo how to do that)?
Question 2: How can I extract / access the autocompletion / code suggestions / argument validation of a specific function? (As this might help to then assign it to another function)
Extract from the DeviceWrapper constructor:
function obj = DeviceWrapper(device,varargin)
% Distinguish between function signatures
if isa(device,"function_handle")
% Signature with function handle to a constructor of a device
% Check whether the input 'device' is a function handle and
% whether it is constructor for a subclass of
% 'devices.device.Device'
assert(devices.device.checkDeviceConstructorFunctionHandleValidity,"devices:device:DeviceWrapper:invalidDeviceConstructor","The function handle provided is not a constructor for a childclass of 'devices.device.Device'! Provide a function handle to a constructor for a device.");
% Create device object
obj.Device = feval(device,varargin{:});
% Code continues here but not relevant for the problem...
end
end
I wrote a functionsSignatures.json file to provide autocompletion for the constructor of the "Device" class. I needed to do that to handle the two different function signatures / prototypes.
functionsSignatures.json file:
{
"_schemaVersion": "1.0.0",
"DeviceWrapper":
{
"inputs":
[
{"name":"DeviceObj", "kind":"required", "type":"Device", "purpose":"Device object that should be attached to the DeviceWrapper object."},
{"name":"Connection", "kind":"namevalue", "type":"Connection", "purpose":"Connection that should be attached to the DeviceWrapper object."}
]
},
"DeviceWrapper":
{
"inputs":
[
{"name":"DeviceConstructorFunctionHandle", "kind":"required", "type":["function_handle","@(args) checkDeviceConstructorFunctionHandleValidity(args)"], "purpose":"Function handle (i.e. '@devices.device.SerialSCPIDevice') of a device constructor that should be used to create a device object and attach it to the DeviceWrapper object."},
{"name":"DeviceConstructorOptions", "kind":"ordered", "purpose":"Options that should be passed to the device constructor. For more information about applicable options refer to the documentation of the provided device constructor."},
{"name":"Connection", "kind":"namevalue", "type":"Connection", "purpose":"Connection that should be attached to the DeviceWrapper object."}
]
}
}
Typical code use. There is no specific autocompletion for the options that might be handed over to the constructor handle of the first argument (please ignore the namespaces, I simplified the example above):

댓글 수: 0
답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!