[a119] Associative or Pseudo- Arrays and Dereferencing Numbers Topic is solved

Discuss the future of the AutoHotkey language
iseahound
Posts: 612
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

[a119] Associative or Pseudo- Arrays and Dereferencing Numbers

29 Jul 2020, 13:54

What is the current intention of pseudo-arrays going forward? For perspective, the last update that introduced useful detection of unset variables is unable to process pseudoarrays correctly.

i.e.

Code: Select all

loop 6
   square%A_Index% := A_Index * A_Index
MsgBox square2
MsgBox square3
MsgBox square6
Additional Question: It seems that using square%2% works, but what does dereferencing a number actually do? square%'2'% is much too ugly to even consider.
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 05:09

iseahound wrote:
29 Jul 2020, 13:54
the last update that introduced useful detection of unset variables is unable to process pseudoarrays correctly.
Indeed, the warning is triggered at parsing (loading time) and these vars exist at runtime. Well, its the same issue as output variables (#45) - we might need some keywords (e.g. dyn and out) to mark dynamically generated variables and output vars so as to skip this warning for them.

I was lucky I changed all my scripts long time ago to use dynamically generated objects (string "obj.objN.objNN.propN" -> obj.objN.objNN.propN) instead of pseudo-arrays and pseudo-objects. There is no loading time warnings for non-existent object properties. U can find str.deref method (getter and setter) in test2.7z archive in the above mentioned topic.

PS: Anyways, for ur task a dynamically created simple array seems to be adequate:

Code: Select all

square:=[]
loop(6)
  square.push(a_index*a_index)
msgbox(square[2])
msgbox(square[3])
msgbox(square[6])
This way it gives u boundary check-ups and commonly used syntax (in other languages).
I always tried to convert pseudo-arrays whenever its possible while converting/updating other ppl scripts cuz I felt they don't belong to AHK V2 and might be phased out eventually.
Helgef
Posts: 4461
Joined: 17 Jul 2016, 01:02
Contact:

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 06:49

It seems that using square%2% works, but what does dereferencing a number actually do?
That doesn't dereference a number,
%Expr% dynamically retrieves a variable by name. The result of the sub-expression Expr must be the name or partial name of the variable to be retrieved.
Cheers.
iseahound
Posts: 612
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 07:29

@lexikos @Helgef Should square%1% syntax be used then? Or stick to correct but ugly square %'1'%? Please give back good old square1.
Helgef
Posts: 4461
Joined: 17 Jul 2016, 01:02
Contact:

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 08:24

Sorry, I don't understand. Both square%1% and square%'1'% (no space) are valid syntax, but neither makes any sense. Use square1.
iseahound
Posts: 612
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 09:21

Right, but on a119 square1 is bringing up warnings saying the variable is unset. I like this new feature, but it does not handle square%A_Index% type syntax well.
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 09:53

iseahound wrote:
30 Jul 2020, 09:21
Right, but on a119 square1 is bringing up warnings saying the variable is unset. I like this new feature, but it does not handle square%A_Index% type syntax well.
Its not possible to check run-time created vars at load time. I would like to keep that warning, too - I found quite a few sleeping errors in my scripts with it. The best we could do (as a temporary workaround) is to have AHK skipping checkups for all the var%var% syntax instances. Or implement some keywords to tell the parser to skip checkups for some variables (that's what I suggest).
Last edited by vvhitevvizard on 30 Jul 2020, 09:57, edited 1 time in total.
Helgef
Posts: 4461
Joined: 17 Jul 2016, 01:02
Contact:

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 09:56

I see your point, sorry. If you are going to refer to the vars both dynamically and non-dynamically, initialise them. I don't think this is a problem.
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 09:59

Helgef wrote:
30 Jul 2020, 09:56
I see your point, sorry. If you are going to refer to the vars both dynamically and non-dynamically, initialise them. I don't think this is a problem.
point is u initialize pseudo-array members at runtime and at load time AHK tries to check every addressing to those not-yet-existing vars. That's why we might need to add some special declarations for them to skip such check-ups.
Helgef
Posts: 4461
Joined: 17 Jul 2016, 01:02
Contact:

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 10:25

point is u initialize pseudo-array members
First, you do not use pseudo-arrays.
we might need to add some special declarations for them to skip such check-ups.
I disagree. If you use dynamic variable references, you are either going to reference them only dynamically, or both dynamically and non-dynamically. For the latter case you will have to initialise them appropriately.
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 10:49

Helgef wrote:
30 Jul 2020, 10:25
First, you do not use pseudo-arrays.
Damn it, u r right again. varX%varY% gives a load-time warning if varY is not initialized somewhere else, but it gives no load-time warnings otherwise (OK).

Code: Select all

i:=a_tickcount ;if we comment this line, it gives a load-time warning on the next line "i: this var appears to never be assigned a value" (OK)
msgbox(array%i%)
or both dynamically and non-dynamically. For the latter case you will have to initialise them appropriately.
I agree on that. So its fine as it is.
User avatar
kczx3
Posts: 1164
Joined: 06 Oct 2015, 21:39

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 13:51

Pseudo arrays should be done with anyways...
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 14:06

kczx3 wrote:
30 Jul 2020, 13:51
Pseudo arrays should be done with anyways...
two thumbs up. its batch-style remnants.

BTW, the only use for %var% syntax I see with:
1. obj.%prop_name% (where prop_name var contains a string, it might be replaced with obj.getprop("string"), obj.setprop("string", value) methods or something like that)
2. %func%() (alternative to func.call(), I use the latter tho).
3. {%nameVar%: valueVar} (non-"literal" property names - this was added recently, but objects with dynamic property names may be populated programmatically anyways).
SOTE
Posts: 1082
Joined: 15 Jun 2015, 06:21

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 14:56

Windows Batch Scripting is still a thing among IT people. So allowing it could be good for easier translation between such scripts, in addition to AHK v1 and WinBatch (an AutoHotkey and AutoIt rival). Other programming languages use the concept of pseudo arrays, like JavaScript, though implementation is different. Pseudo arrays are also easier to understand for non-programmers getting into the language. I totally understand that "real or professional" programmers would thumb their noses at such, but part of AutoHotkey's appeal is to allow for and even target such newbies to programming. Kind of like that utility language to just get stuff done without all the complications. Psuedo arrays would arguably also be good for unique, unusual, or creative situations.
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 15:04

SOTE wrote:
30 Jul 2020, 14:56
Windows Batch Scripting is still a thing among IT people.
It is. IT companies might keep legacy code for compatibility reasons mainly.

Code: Select all

for /d %%i in ("%PROGRAMFILES%") do (
    set PROGRAMFILESSHORT=%%~si
)
for /f "eol=# delims== tokens=1,2" %%i in (%SETTINGSFILE%) do (
    set %%i=%%j
)
if %1 == 1 (
    exit /b
)
set /a PARAM=%1 - 1
call :factorial %PARAM%
"Lovely" syntax, isn't it? We still have AHK V1 for that.

For scripting the trend is to use javascript-like syntax over that nonsense.
swagfag
Posts: 4026
Joined: 11 Jan 2017, 17:59

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 15:25

1. the point is, its a shorthand. otherwise theres already Obj.DefineProp(prop_name, {Value: value}) and Obj.GetOwnPropDesc(prop_name).Value
2. except %fn%() and fn.Call() do different things
3. isnt {%nameVar%: valueVar} "populating a property programatically"?
what sucks about % is its got no sidedness
oh em gee, son, back at it again with those winbatch callbacks
Psuedo arrays would arguably also be good for unique, unusual, or creative situations.
arguably is arguably the only thing we can agree on here
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 15:32

swagfag wrote:
30 Jul 2020, 15:25
3. isnt {%nameVar%: valueVar} "populating a property programatically"?
By populate programmatically I meant loop with obj.keyN:=valueN over obj:={"keyN":"valueN"} JSON-like syntax.
BTW, I don't understand what was the reason to replace non-literal key names (vars containing property names) like obj:={keyN:"valueN"} with obj:={%keyN%:"valueN"}?
the point is, its a shorthand.
Oh, I missed that defineprop method. It does seem way too verbose (as the majority of Lexikos' recently added functions). Well, %var%DOS environment variables-like syntax feels bad.
Last edited by vvhitevvizard on 30 Jul 2020, 15:43, edited 1 time in total.
swagfag
Posts: 4026
Joined: 11 Jan 2017, 17:59

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 15:42

keyN in obj:={keyN:"valueN"} never used to lookup a variable value to set as a key
previously, u could either do obj:={keyN:"valueN"} or obj:={"keyN":"valueN"} to define a property with the literal name obj.keyN;
or (iirc) obj:={(keyN):"valueN"} to define a property with the value of the variable keyN
now, u can only do obj:={keyN:"valueN"} to define a property with the literal name obj.keyN;
or obj:={%keyN%:"valueN"} to define a property with the value of the variable keyN
anything else is a syntax error. why is that u may ask? @lexikos probably has a grander vision for where the parser development is headed
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 15:45

@swagfag
2. except %fn%() and fn.Call() do different things
Could u elaborate please? The current documentation even puts both syntax variations together:
Func.Call(Param1, Param2, ...)
%Func%(Param1, Param2, ...)
https://lexikos.github.io/v2/docs/objects/Func.htm#Call
swagfag
Posts: 4026
Joined: 11 Jan 2017, 17:59

Re: [a119] Associative or Pseudo- Arrays and Dereferencing Numbers

30 Jul 2020, 15:49

https://lexikos.github.io/v2/docs/Objects.htm#Meta_Functions
Meta-functions are not called in the following cases:

x[y]: Using square brackets without a property name only invokes the __Item property.
%x%(): Calling the object itself only invokes the Call method. This includes internal calls made by built-in functions such as SetTimer and Hotkey.
Internal calls to other meta-functions or double-underscore methods do not trigger __Call.

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 5 guests