Access a class member as a normal variable? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Access a class member as a normal variable?

Post by Coiler » 24 Mar 2021, 13:53

Is it possible to access a variable stored within a global class instance as a variable that can be passed to functions as a reference (&object.property)?

Here's what my actual code would look like. The last parameter is where I'm a little stuck. I want that variable to behave as though it is a global variable - so its state can change at runtime and be reflected when the HotIf() is checked. Is this possible? Or would I have to remove all such variables from classes when I want to do this?

Code: Select all

HotIf( (hkey) => HotMain( app_exe, key_chain, hkey, &Chain.User ) )
In this code, Chain is the global class instance, and User is a string member stored within it. Originally, I was just using the variable by accessing it directly from the test function (HotMain()). But I'm wanting to allow two different scenarios, each one will rely on a different property of Chain.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Access a class member as a normal variable?  Topic is solved

Post by swagfag » 24 Mar 2021, 20:41

u cant &object.property, https://lexikos.github.io/v2/docs/Functions.htm#ByRef
Known limitations:
  • It is not possible to construct a VarRef for a property of an object (such as foo.bar), A_Clipboard or any other built-in variable, so those cannot be passed ByRef.
what problems exactly did u have with

Code: Select all

HotIf( (hkey) => HotMain( app_exe, key_chain, hkey, Chain.User ) )

User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Re: Access a class member as a normal variable?

Post by Coiler » 02 Apr 2021, 19:41

I'm never sure how variables are going to be copied in closures. I know temporaries become like static values, and globals become like references, but I'm not sure how class members behave yet. I will give that a try. Yeah, after thinking about it, I'm not sure why I didn't realize this would work. Thanks!

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Access a class member as a normal variable?

Post by lexikos » 03 Apr 2021, 22:25

I know temporaries become like static values, and globals become like references, but I'm not sure how class members behave yet.
I think by "temporaries" you mean non-static local variables. They become "like static values" because they retain their value between calls to the closure. But I think maybe you're overthinking it, or maybe not generalizing enough.

Closures capture variables by reference - you could say - regardless of whether they are local, global or static.
  • If the variable is global, the closure has a reference to a global variable.
  • If the variable is static, the closure has a reference to a static variable.
  • If the variable is local, the closure has a reference to a local variable.
There's meant to be fundamentally no difference between these cases as far as the closure is concerned. The closure just has a reference to a variable. Every time you call the same closure, it still has the same references, so they all act like global or static variables or properties of the closure itself.

You might distinguish between local variables as temporary and global or static variables as persistent, but there's a better perspective: like objects, any variable exists for as long as someone has a reference to it. By default, local variables are only referenced by their name until the function returns. Closures capture references to the outer function's local variables, keeping hold of them until the closure itself is freed.

You can also make more references to a local variable explicitly. For instance, return &localVar returns the local variable by reference to the caller, allowing the caller to retrieve or assign it. Alternatively, your function could explicitly capture a variable by taking its reference and binding it to a parameter, like this: ((&localVar) => ...).Bind(&localVar). Now localVar refers to the same variable in both the outer function and the inner function, but the inner function is a BoundFunc rather than a Closure.

Closures capture variables. "Class members" have no relevance to closures. Your closure captures Chain, not Chain.User.

Post Reply

Return to “Ask for Help (v2)”