What is happening there?
Looking at the class variables documentation I see no mention of this behavior.
Can someone explain what is going on?
Note:
This does not happen in v2. as both class variables and instance variables give the same error shown above.
Re: static continuation to object... is that documented?
Posted: 12 May 2022, 11:52
by iseahound
The continuation section is fed to the preprocessor, so any "continuations" are removed and replaced before the script starts executing the code. You're seeing a difference with static because you didn't instantiate an instance of the class. You can use continuation sections anywhere, for any purpose, because they have zero interaction with code (except for maybe directives).
Re: static continuation to object... is that documented?
class test
{
myVar := "
(Ltrim Join
{
""info"": 1,
""test"": 2
}
)"
}
msgbox % test.myVar
Try that code and tell me what you get.
Re: static continuation to object... is that documented?
Posted: 13 May 2022, 09:31
by teadrinker
I'm on phone now, can't test, but in this case myVar is an instance var, so you must use the keyword new.
Re: static continuation to object... is that documented?
Posted: 14 May 2022, 00:42
by Helgef
If I'm not mistaken, in v1, static class vars share implementation with static function vars, for which this is documented. But you should use := anyways.
Cheers.
Re: static continuation to object... is that documented?
Posted: 14 May 2022, 01:41
by Xeo786
This issue was mentioned as autohotkey-oofs by Run1e, there no mentioned about Static/class variables
Spoiler
I don't know this is known to anyone else or I might accidentally discovered this for class Static Var. BUT
That behavior confused me for a while but I get hang of it, I intend to pass JSON txt directly to winhttp request here, but when this behavior gave me AHK object, which then I passed using Json.dump() as winHTTP accepts text and also faced issue of as AuroHotkey v1 has no distinct data types affecting Json parsing therefore I also fixed that using Payloadfix()
TLDR wrote:
My Intention was to pass Json txt and receive Json txt (no Json.dump() but json.load()) as response, but above behavior mentioned by @RaptorX changed what Rufaydium now uses Json.dump() object and Json.load(request), thanks you class static var behavior, which I realized simple and useful
Static declarations are evaluated only once, before the auto-execute section, .....*
[v1.1.08+]: Declarations like static x.y := z are also supported, provided that x was previously declared in this class. For example, static x := {}, x.y := 42 declares x and also initializes ClassName.x.y.
I think ahk pointer start executing static class vars as ahk script reading object and fortunately Json in text is same as we define predefined objects in ahk x := {"x":{"y":2}}
I suspect this behavior will not supported ahk version below [v1.1.08+] its just my assumption.
z := {"x":{"y":2}}
msgbox, % z.x.y
z :=
( LTrim Join
{"x":{"y":2}}
)
msgbox, % z.x.y
class a
{
static t :=
( LTrim Join
{"x":{"y":2}}
)
}
msgbox, % a.t.x.y
class b
{
static t =
( LTrim Join
{"x":{"y":2}}
)
}
msgbox, % b.t.x.y
Re: static continuation to object... is that documented?
Posted: 14 May 2022, 05:06
by lexikos
@RaptorX
You are not guaranteed to be given a sensible error message, or any at all, when you do something invalid. This is especially true in v1. Declaring a class variable with = is not valid.
@Xeo786
It's not clear to me what you're trying to say, but note that the documentation you quoted has no relevance to the code in your post, as you did not use any "Declarations like static x.y := z". The sub-expression{"x":{"y":2}} can be used in any expression, and class variable initializers use expressions. It consists of two separate operations: one creating an object with "y" property, and the next (moving outward) creating an object with "x" property.
Re: static continuation to object... is that documented?
@Xeo786
It's not clear to me what you're trying to say, but note that the documentation you quoted has no relevance to the code in your post, as you did not use any "Declarations like static x.y := z". The sub-expression{"x":{"y":2}} can be used in any expression, and class variable initializers use expressions. It consists of two separate operations: one creating an object with "y" property, and the next (moving outward) creating an object with "x" property.
What I was trying to say, was all my assumptions, now I find Helgef's argument correct according to your replay, thanks that cleared my mind,
class A
{
static C =
( Ltrim Join
"
So '=' here is acting like ':='
"
)
}
msgbox, % A.C "`nthis is why string should be covered with by " chr(34) chr(34) "`nand Json accepted just like Object assigning"
Re: static continuation to object... is that documented?
If I'm not mistaken, in v1, static class vars share implementation with static function vars, for which this is documented. But you should use := anyways.
Cheers.
I see no mention in that documentation of how a continuation section would return an object instead of a string though. I would have always assumed a continuation section would always return a string based on what you linked.
Re: static continuation to object... is that documented? Topic is solved
Posted: 16 May 2022, 12:26
by Helgef
Method #2: [...] it can also be used with any command or expression.
Never thought it could be for an object, but you are right, that is vague enough to allow for an object to be created with a continuation section. Weird indeed.
Re: static continuation to object... is that documented?
Posted: 17 May 2022, 23:04
by lexikos
A continuation does not "return" anything, and has nothing to do with the result of the code. It is just a general mechanism for spreading code across multiple lines. If you write static myVar = {"info": 1, "test": 2}, you will get the same result.
Re: static continuation to object... is that documented?
A continuation does not "return" anything, and has nothing to do with the result of the code. It is just a general mechanism for spreading code across multiple lines. If you write static myVar = {"info": 1, "test": 2}, you will get the same result.
which is what is throwing me off because generally speaking in v1 the = operator is for strings, so I wouldnt expect the code you posted to create an object, but rather be a string.
This is especially true in v1. Declaring a class variable with = is not valid.
But as I understand by your previous comment inside classes the = operator is not behaving as I would expect since it is not considered valid.
Re: static continuation to object... is that documented?
global x = 1 + 1
y := 1, w = z = 1 + 2
ListVars
if (x = 1 + 1)
MsgBox Above: = not operating on a string.
These are documented (for v1).
But as I understand by your previous comment inside classes the = operator is not behaving as I would expect since it is not considered valid.
I told a small lie. Strictly speaking, it is not defined in the documentation, but it was designed that way specifically for consistency with declarations used in functions. Just don't do it. Use the correct, well-defined syntax.
However, your expectations were also mistaken. Legacy assignments are Var = Value, not Static Var = Value.