Modify

Opened 4 years ago

Last modified 4 years ago

#3003 new Bug

Using a function call in an array assignment causes 2 function calls

Reported by: jguinch Owned by:
Milestone: Component: AutoIt
Version: 3.3.12.0 Severity: None
Keywords: Cc:

Description

With this code :

Local $aArray[1]

$aArray[ _myFunction() ] = "myString"

Func _myFunction()
	ConsoleWrite("_myFunction" & @CRLF)
	Return 0
EndFunc

"myFunction" appears two times in the output console, but it should once.

Attachments (0)

Change History (6)

comment:1 Changed 4 years ago by jguinch

Also, this code is a good example of what happends :

Local $aArray[1]
$aArray[  0 * MsgBox(0, "", "Appears twice")  ] = MsgBox(0, "", "Appears once")

comment:2 follow-up: Changed 4 years ago by Melba23

Interesting that it does not do the same when you set the function to a variable:

Local $aArray[1]

$aArray[ _Function() ] = "myString"

ConsoleWrite(@CRLF)

$hFunction = _Function()
$aArray[ $hFunction ] = "myString"

Func _Function()
	ConsoleWrite("_Function @ " & @MSEC & @CRLF)
	Sleep(100)
	Return 0
EndFunc

M23

comment:3 in reply to: ↑ 2 Changed 4 years ago by anonymous

Replying to Melba23:

Interesting that it does not do the same when you set the function to a variable

Not very interesting. You assign the return value of the function to a variable and then evaluate that variable expression (twice).

comment:4 Changed 4 years ago by jguinch

I don't really understand what you mean in comment3...

comment:5 Changed 4 years ago by BrewManNH

You're calling the function to get the returned value for the array assignment, then you're calling it again when you actually set the value to the array. Run this modification to your code to see what's happening.

#include <Array.au3>
Local $aArray[2]

$aArray[ _myFunction() ] = "myString"
_ArrayDisplay($aArray)
Func _myFunction()
    Local Static $Return = -1
	ConsoleWrite("_myFunction = " & $Return & @CRLF)
    $Return += 1
	Return $Return
EndFunc

It calls the function the first time to see what element of the array to set it to, then when AutoIt does the actual declaration for the element it calls it again to see where it's going. You'll notice that the only element with anything in it is $aArray[1] because that's what the return value is when you set the string to the array.

So, I don't know if this is a bug, but it looks like a bad idea to use a function call to determine where in an array to put something, at least don't do it in the actual declaration statement, because you won't be able to be 100% sure it's going where you think it's going.

comment:6 Changed 4 years ago by anonymous

@BrewManNH : you pointed exatly what I was trying to do to assist a forum user in this thread : http://www.autoitscript.com/forum/topic/169135-flexibility-with-declaring-an-array/

I wanted to post this kind of code :

#Include <Array.au3>

Local $a[10]

$a[ _i() ] =  "First row"
$a[ _i() ] =  "Second row"
$a[ _i() ] =  "Third row"

_ArrayDisplay($a)


Func _i() 
	Local Static $iIndex = -1
	$iIndex += 1
	Return $iIndex
EndFunc

But when I saw the result, I decided to write a ticket here.
For me, it is a bug, but you decid :-)
If not, maybe something about this "bad" uasge should appear in the help file

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as new The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.