Suppose we have Check.m:
classdef Check < handle
methods (Static)
function doStuff()
if isCalledFromAssertSubclass
% do this stuff only if called as Assert.doStuff(), not if called as Check.doStuff()
end
% do other stuff
end
end
end
and Assert.m:
classdef Assert < Check
% nop
end
As written in the comment, I would like to have that Check.doStuff()
executes only the "do other stuff" line and that Assert.doStuff()
executes the if isCalledFromAssertSubclass
block as well.
I want to use static methods, so that I don't neet to create an Assert object whenever I need an assert. Using a global assert object is also very ugly and needs a global assert
line in each function I want to use an assert. Likewise for checks.
So there are two problems:
- Since these are static classes, there is no chance of using
class(obj)
or any other non-static class property or function. dbstack
is not inheritance aware and always returnsCheck
as calling class, also forAssert.doStuff
.
I did find a working solution, which uses a combination of dbstack
and dbtype
to read the line where the call came from, i.e. the line where it says Assert.doStuff()
. However it involves two debug functions which probably should not be used in productive code and more importent, dbtype is very slow (in my case, 30 our of 70 seconds!).
I could use a package instead (directory +Check
with function files in there) and create a symlink +Assert -> +Check
. Then I could check the file name, but that's a) not portable, b) quite ugly and c) also somewhat slow (I suppose).
Is there any faster method for this?