super-global access from class static __new()
Posted: 30 Sep 2020, 12:33
A very large code base I maintain relies on certain "super-globals" that create "constants", on which nearly every function/method depends either directly or indirectly. For example:
Wherever na is used throughout thousands of functions/methods, none of these references ever use the global declaration because it's not required. They're super-globals! (As I've said in prior posts, it would be nice to be able to use these super-globals as parameter defaults too, but I've sort of given up on that one...)
So here's the problem... We know that static __new() for each class executes before any of the main body (autoexec) code, in the order classes are defined. Makes sense, except that some of my class initializers actually do stuff that requires other functions, so at least certain of the super-globals need to be set up first.
So I thought... no problem... since the class initializers execute in the order classes are defined, I'll just set up the super-globals in their own static class initializer which appears at the very top of the code. So I tried various incarnations of the following at the top of my code:
Of course, I quickly realized this doesn't create a super-global, since it's inside a function body. So I'm back to the drawing board. My options, as I currently perceive them, are:
Code: Select all
global na := '`vna' ;sentry for missing parameter
So here's the problem... We know that static __new() for each class executes before any of the main body (autoexec) code, in the order classes are defined. Makes sense, except that some of my class initializers actually do stuff that requires other functions, so at least certain of the super-globals need to be set up first.
So I thought... no problem... since the class initializers execute in the order classes are defined, I'll just set up the super-globals in their own static class initializer which appears at the very top of the code. So I tried various incarnations of the following at the top of my code:
Code: Select all
class script_init { ;class whose only purpose is the static __new()
static __new() {
global
global na := '`vna' ;sentry for missing parameter
}
}
- rewrite all code to explicitly declare globals and never rely on super-globals (not a great option!)
- don't do stuff that calls functions (that need super-globals) in class initializers (but I need to!)
- fix just the functions called by class initializers to make explicit global declarations (introduces something that's not uniform across all functions though)
- create routines in non-static (instance) __new that, on first run only, set up the required stuff I would have done in static __new... which required functions... which required super-globals... which have now already been defined... (but that seems like an ugly proxy for the actual purpose of static __new)
- find some way to declare super-globals from a class initializer (...which is why I'm here asking for help)