Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?
-
Does it matter where in HISE Script I declare identifiers?
Stuff like:
const var panel_TM_REMIC_ToggleSystem_GUI = Content.getComponent("panel_TM_REMIC_ToggleSystem_GUI");
combo_TM_REMIC_MicSelectionNew_GUI.setControlCallback(on_TM_NewMicSelector_Control);
I've seen people write that variables should be declared in the onInit callback, but this seems ungainly and decreases code portability, especially with large projects.
Then again, I've noticed different behaviour in HISE Script depending on the order I declare things.
Also - does it matter what order I include external script files?
include "names_of_ferrets.js"; include "list_of_ferrets.js";
I've seem to have noticed a difference here, as well - as far as I know, there's no header mechanism, though.
Thanks!
-
@clevername27 said in Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?:
I've seen people write that variables should be declared in the onInit callback, but this seems ungainly and decreases code portability
Use namespaces
@clevername27 said in Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?:
Also - does it matter what order I include external script files
Yes, at least if those files contain namespaces
-
-
@d-healey You are a gentleman and scholar. I"m just not quite there yet - I understand namespaces and HISE's implementation - forgive me for being a little dense, but I'm unclear as to the answer to my question, and how namespaces is related? š§
-
-
Ok so when you have a namespace, for example
namespace MyNamespace { }
Everything inside of it is essentially within
on init
but its encapsulated and as long as you don't declarevars
in it the scope of its variables is contained within. This keeps things neat and tidy.Unfortunately we can't nest namespaces, so for situations where you want to declare some enums within a namespace I use a const object. e.g.
namespace MyNamespace { const MY_ENUMS = { MY_FIRST_ENUM: 1, MY_SECOND_ENUM: 2, MY_THIRD_ENUM: 3 } }
So then I can access this from anywhere in the script using
MyNamespace.MY_ENUMS.MY_SECOND_ENUM
keeping everything nice and neat and making it easy to identify where a particular variable has been declared.For other namespace variables that I want to be accessible from outside the namespace (although I try to avoid that situation) I use getter and setter functions. Of course you could just access a variable directly but a getter and setter is better because if the variable name changes or the way to access them changes you only need to update that in the namespace and the other places that access them don't need to know anything about it.
Regarding the order of includes. If you have an include with a namespace, and a second include with a namespace that references something in the first namespace, then they must be included in order.
-
@d-healey Thank you for your extended explanation. Do I have this correct, thenā
If I declare a constant after it's referenced, I'll have a problem. But I declare it in exactly the same place within a namespace, it will be fineābecause before HISE executes anything, it goes through all the scripts and looks for things defined in namespaces (and builds the symbol table)?
-
@clevername27 said in Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?:
If I declare a constant after it's referenced, I'll have a problem.
Yes
@clevername27 said in Does the Order of Declaring Identifiers (Variables, Functions, etc.) Matter?:
But I declare it in exactly the same place within a namespace, it will be fine
No
I was saying if you have two namespaces in two separate files, and one namespace makes reference to the other, then you must put them in the correct order so that the reference is after the declaration.
For example this works:
But this doesn't
-
@d-healey Thank you for taking the time to clarify all this - cheers.
-