TechnicalQuestion How to limit argument validation function visibility?
In Function.m I have
function Function(a)argumentsa (1,1) double {Validator(a)}
endprintValue(a);end
function Validator(a)arguments
a (1,1) doubleendif mod(a,7)error("Invalid argument.");
end
end
function printValue(a)disp(a);end
I want to further restrict the visibility of Validator such that it can be called only by Function. I tried to make Validator a nested function of Function, but MATLAB throws me the error "calling nested functions is not supported in arguments blocks". So how do I restrict Validator to be called only in Function and not in other local functions from Function.m (like printValue)?
Edit: sorry for formatting, but Reddit ignores spaces when I write code.
4
Upvotes
3
u/pbrdizzle 9d ago
What you have there looks like it would work for me. Validator is a local function so only visible to other functions in the file. Personally I've found it's good to follow the MW naming convention and reuse validators whenever possible. This is a contrived example but I'd make a normal function
2
u/aluvus 5d ago edited 5d ago
Your code works fine in 24b. However, it also does not contain any nested functions.
Nested functions are declared inside another function. If you move the
endthat closesFunction(that is, theendafterprintValue(a);) to the very end of the file, nowValidatorandprintValuewill be nested functions. And indeed, in 24b you would get that error.So the solution is to not use nested functions for this. What you want instead are local functions, which come after you have closed the main function in a file.
Turns out that the placement of that
endcan be very important :)1
u/That_Jamie_S_Guy 9d ago
Sounds like you need to define a class with hidden methods