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

Talk about things C/C++, some related to AutoHotkey
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

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

Post by jeeswg » 10 Jun 2019, 23:20

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

#Warn
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:

VarIsInit(Var)

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

==================================================

[script.h]

BIF_DECL(BIF_VarIsInit);

==================================================

[script.cpp]

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

==================================================

[script2.cpp]

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

==================================================
[EDIT:] Implemented as IsSet in AHK v2:
Added IsSet(Var). · Lexikos/AutoHotkey_L@916fe75 · GitHub
https://github.com/Lexikos/AutoHotkey_L/commit/916fe75fb3670a9884276ba2c0f88d39808f6d9f
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “C/C++”