Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

function static clears after class statics are assigned?


  • Please log in to reply
7 replies to this topic
adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012
This confused me when I first saw it. Any explanations as to what is happening?
class test
{
    static x := test.fn()
    fn()
    {
        tta("Hello")
    }
}
tta(" there")
;global tta_text
tta(text, clear = false)
{
    static tta_text =  ; static clears after class statics are assigned?
    if (clear)
        tta_text =
    tta_text .= text
    ToolTip, %tta_text%
}

hex(number, length, keepAsNumber=1)
{
    oldFormatInteger := A_FormatInteger
    SetFormat, IntegerFast, H
    number+=0
    if (!keepAsNumber)
    {
	  number := substr(number, 3)
	  loop % length - strlen(number)
		  number := "0" number
    }
    SetFormat, integer, %oldFormatInteger%
    return number
}

Escape:: ExitApp
The code should put up a tooltip that says: "Hello there", instead it puts " there". The function static variable is being reset. It also happens if made into a global. If I don't give it a default value, it doesn't cause a problem though or if I put it outside as a super global.

Thanks,


Adrian

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

[AHK_L 58+]: Static var := expression is supported. All such expressions are evaluated immediately before the script's auto-execute section in the order they are encountered in the script.


Posted Image

tta(text, clear = false)
{
static tta_text = ; static clears after class statics are assigned?
if (clear)
tta_text =
tta_text .= text
ToolTip, %tta_text%
}
class test
{
static x := test.fn()
fn()
{
tta("Hello")
}
}
tta(" there")
Sleep, 3000
ExitApp

Prefer ahkscript.org for the time being.


adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012

[AHK_L 58+]: Static var := expression is supported. All such expressions are evaluated immediately before the script's auto-execute section in the order they are encountered in the script.

Yes, it is encountered first when it evaluates the class static member. It is then encountered at the point of the tta(" there") call. The thing is, we are defining the order in different ways. I, by the order that they are encountered in the execution of the script. You, by the order that they are located in the script file. Subtle, but significant.

Can this be clarified in the help please?

Thanks,


Adrian

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
Static declarations are processed in the order that the pre-parser encounters them - top to bottom. Static declarations are not subject to control flow.

adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012
I understand that Lexicos, I'm just saying that it should be clarified in the doc.

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


Lexicos
  • Members
  • 20 posts
  • Last active:
  • Joined: 05 Jan 2009

I, by the order that they are encountered in the execution of the script.


Is it paradoxical to say that static declarations are evaluated in the order that they are encountered in the execution of the script, when execution of the script begins with evaluating static declarations?

adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012

Is it paradoxical to say that static declarations are evaluated in the order that they are encountered in the execution of the script, when execution of the script begins with evaluating static declarations?

I'm not sure what you mean. Perhaps you could show an example?

I have found that when I run my script, all class statics are evaluated first, then the script is executed as I would expect. Example:
z := fn("there")

fn(y)
{
    static x := "Hello"
    xx := x
    x := y
    return xx
}

MsgBox % "'" z "' '" fn("") "'"
Outputs a message box that says 'Hello' 'there'.
The following examples do things that were totally unexpected:
class xx
{
    static z := xx.fn("there")
    fn(y)
    {
        static x := "Hello"
        xx := x
        x := y
        return xx
    }
    ; static z := xx.fn("there") ; Doesn't matter if before or after the function xx.fn() definition
}

MsgBox % "'" xx.z "' '" xx.fn("") "'"
outputs a message box that says '' '' and:
fn(y)
{
    static x := "Hello"
    xx := x
    x := y
    return xx
}
class xx
{
    static z := fn("there")
}

MsgBox % "'" xx.z "' '" fn("") "'"
outputs '' 'there' and:
class xx
{
    static z := fn("there")
}
fn(y)
{
    static x := "Hello"
    xx := x
    x := y
    return xx
}

MsgBox % "'" xx.z "' '" fn("") "'"
outputs '' 'Hello' and:
class xx
{
    fn(y)
    {
        static x := "Hello"
        xx := x
        x := y
        return xx
    }
}
z := xx.fn("there")
MsgBox % "'" z "' '" xx.fn("") "'"
outputs 'Hello' ''.

The evaluation order of statics is not just limited to where they physically are in the file, but whether they are class statics or function statics. A class static referencing a function static will always get an undefined value as class statics are evaluated prior to anything else it appears. I'm unable to reconcile the class function static example though.


Adrian

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010

Hello AdrianH,

 

Here is how I understand one of the examples:

 

The example:

class xx
{
    static z := xx.fn("there")
    fn(y)
    {
        static x := "Hello"
        xx := x
        x := y
        return xx
    }
    ; static z := xx.fn("there") ; Doesn't matter if before or after the function xx.fn() definition
}
MsgBox % "'" xx.z "' '" xx.fn("") "'"

What happens:

  • class xx
  • static z
  • static x
  • super-global object XX is declared and initialized with an object, static key z is declared, static variable x is declared
  •  
  • static z := xx.fn("there")
  • The static key z is initialized with the expression: xx.fn("there"), the method xx.fn() is called
  •  
  • fn(y)
  • static x := "Hello"
  • this line isn't called at all at the point of _this_ particular instance of the function call, thus x is an empty, non-static variable
  •  
  • xx := x
  • xx _super-global_ variable gets an empty string. This is the key point of the program. I don't think your intention was to overwrite the class object at this point. xx is a super-global variable, because super-global variables don't have to be declared inside of functions with the global keyword. xx could be renamed to something else.
  •  
  • x := y
  • x gets the value of the function parameter "there"
  •  
  • return xx
  • an empty string is returned
  • z := xx
  • the z key gets the empty string value (return from the fn)
  •  
  • static x := "Hello"
  • static variable x gets the value "Hello"
  •  
  • Auto-execute section of the script begins executing
  •  
  • MsgBox % "'" xx.z "' '" xx.fn("") "'"
  • at this point, xx is not an object and the base object neither has the 'z' key nor the 'fn' method, so nothing really happens. But lets suppose xx.fn("") is called.
  •  
  • fn(y)
  • xx := x
  • because the static variable x is now initialized, xx gets the value of "Hello"
  • x := y
  • x gets the value "" which was the argument of the fn
  •  
  • return xx
  • the string "Hello" is returned.

 

Thanks for the effort on providing the examples and a clear description. Pingback.

//edit-20150122: fixed the Pingback link: Pingback.


Edited by trismarck, 22 January 2015 - 08:05 AM.

New Autohotkey forum: http://ahkscript.org.