Page 1 of 1

C++: AHK source code: VarIsInit (IsSet)

Posted: 10 Jun 2019, 23:20
by jeeswg
Here is some code for a potential VarIsInit function, it determines whether a variable has been initialised or not.

Note: possible 'Var' functions: VarExist/IsVar, VarGetName, VarIsInit/IsInit, VarSet/Assign, VarSwap/Swap.

- One very useful feature of #Warn is that it can warn you if you use a variable that hasn't been assigned a value.
- The drawback of this feature is that you must define the initial values somewhere, e.g. the auto-execute section, to avoid the warning, this separates the initial values from the code proper.
- By using VarIsInit, you could check if the variables were initialised, and if not, assign a value. This avoids the #Warn error messages, and allows you to keep the variable initialisations within the code proper.

Example usage:

Code: Select all

;test VarIsInit prototype

var := 1
MsgBox, % VarIsInit(var) ;1
MsgBox, % VarIsInit(var2) ;0
obj := []
MsgBox, % VarIsInit(obj) ;1
MsgBox, % VarIsInit("abc") ;(blank)
MsgBox, % VarIsInit(123) ;(blank)
MsgBox, % VarIsInit(123.456) ;(blank)

;note: all built-in variables returned blank except A_Args and ErrorLevel
MsgBox, % VarIsInit(A_Args) ;1
MsgBox, % VarIsInit(A_ScriptHwnd) ;(blank)
MsgBox, % VarIsInit(A_Tab) ;(blank)
MsgBox, % VarIsInit(Clipboard) ;(blank)
MsgBox, % VarIsInit(ClipboardAll) ;(blank)
MsgBox, % VarIsInit(ComSpec) ;(blank)
MsgBox, % VarIsInit(ErrorLevel) ;1
MsgBox, % VarIsInit(False) ;(blank)
MsgBox, % VarIsInit(ProgramFiles) ;(blank)
MsgBox, % VarIsInit(True) ;(blank)

MsgBox, % var3 ;error message is #Warn on

Code: Select all


for AHK v1: function names and parameters:


- note: relevant code in IsByRef
- note: returns blank for a non-variable
- note: perhaps A_ variables should return 2 or A or B






	else if (!_tcsicmp(func_name, _T("VarIsInit")))
		bif = BIF_VarIsInit;
		min_params = 1;
		max_params = 1;



	if (aParam[0]->symbol != SYM_VAR)
		// Incorrect usage: return empty string to indicate the error.
		aResultToken.symbol = SYM_STRING;
		aResultToken.marker = _T("");
		aResultToken.value_int64 = !(aParam[0]->var->IsUninitializedNormalVar());

[EDIT:] Implemented as IsSet in AHK v2:
Added IsSet(Var). · Lexikos/[email protected] · GitHub