필터 지우기
필터 지우기

argument suggestion with shared argument validator

조회 수: 4 (최근 30일)
guillaume deguyon
guillaume deguyon 2023년 5월 30일
댓글: guillaume deguyon 2023년 8월 8일
Hello matlab community.
I am facing a problem with function argument validation and auto-completion. I have a set of static methods which share the same argument validation. In the following example, the input argument must belong to ["a", "b"].
I cannot find a way to have a common definition of this vector membership without losing auto completion:
classdef dummyClass
properties (Constant)
m = ["a", "b"];
end
methods (Static)
% this method works but membership vector is hardcoded
function s = fun1(a)
arguments
a {mustBeMember(a,["a", "b"])} = "a"
end
s = a;
end
% using a third party validator does not trigger auto completion
function s = fun2(a)
arguments
a {mustBeAorB(a)} = "a"
end
s = a;
end
% yield an error as dummyClass.m is not defined
function s = fun3(a)
arguments
a {mustBeMember(a,dummyClass.m)} = "a"
end
s = a;
end
function mustBeAorB(a)
arguments
a {mustBeMember(a,["a", "b"])} = "a"
end
end
end
end
There is apparently a solution using json file, but I would like to avoid it if possible.
Do you have any suggestion ?

답변 (2개)

Sugandhi
Sugandhi 2023년 6월 9일
Hi,
I understand that you are facing a problem with function argument validation and auto-completion.
The possible work around could be, to define the valid membership vector as a class constant instead of a property constant. This way, you can reference the vector using the class name in instances where the auto-completion is lost.
Here's an updated version of your code that implements this approach:
classdef dummyClass
methods (Static)
% Define valid membership vector as a class constant
function m = validMembership()
m = ["a", "b"];
end
% Use validMembership class constant to validate input argument in the mustBeMember constraint
function s = fun3(a)
arguments
a {mustBeMember(a,dummyClass.validMembership())} = "a"
end
s = a;
end
end
end
In the code above, the valid membership vector is now defined as a class constant within a static function called 'validMembership'. 'The mustBeMember' constraint used in `fun3` references this class constant by calling `dummyClass.validMembership()`.
This approach provides a common definition for the valid membership vector without hard coding the values or relying on a third-party validator. It also retains auto-completion in the function when typing `dummyClass.`.
  댓글 수: 1
guillaume deguyon
guillaume deguyon 2023년 6월 27일
Hi, thank you for your answer.
Using the membership function as you suggest, I get a similar error than if I use a constant property:
It won't accept anything that is not literal or previously defined arguments.
I tried it on both matlab 2022b and 2023a.
I also came to this json solution for code suggestion, but it adds maintenance workload and does not solve the problem of having a unique definition of the membership vector

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


VBBV
VBBV 2023년 8월 7일
편집: VBBV 2023년 8월 7일
function s = fun3(a)
          arguments
              a {mustBeMember(a,@dummyClass)} = "a"
          end
          s = a;
end

Did you try calling the dummyClass as above ?

  댓글 수: 1
guillaume deguyon
guillaume deguyon 2023년 8월 8일
I tried. But I get the same error: it only accepts previously declared arguments or literals.
I also tried to define it via a non static method, so the class object is an argument of the function. But it refuses to access a property of the object:

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

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by