Page 1 of 1

static continuation to object... is that documented?

Posted: 12 May 2022, 09:41
by RaptorX
Hello guys.

So this one is really weird and interesting to me.

in AHK v1 (latest version) this code creates a variable with a JSON string in it:

Code: Select all

myVar =
(Ltrim Join
{
	"info": 1,
	"test": 2
}
)

msgbox % myVar
if we do the same in a class, we get an error because that is not a valid definition of a property for some reason:

Code: Select all

class test
{
	myVar =
	(Ltrim Join
	{
		"info": 1,
		"test": 2
	}
	)
}
msgbox % test.myVar
error
This is where it gets interesting, by adding the static modifier to have a class variable, not only it starts working, but suddenly I have an object??

Code: Select all

class test
{
	static myVar =
	(Ltrim Join
	{
		"info": 1,
		"test": 2
	}
	)
}
msgbox % isObject(test.myVar) ? "True" : "False"
msgbox % test.myVar.info
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?

Posted: 12 May 2022, 13:36
by RaptorX
iseahound wrote:
12 May 2022, 11:52
You can use continuation sections anywhere, for any purpose, because they have zero interaction with code (except for maybe directives).
In the second code I get an error though, so it seems I cant use it there.

Re: static continuation to object... is that documented?

Posted: 12 May 2022, 14:46
by teadrinker
You can use expression syntax:

Code: Select all

class test
{
   static myVar := "
   (Ltrim Join
   {
      ""info"": 1,
      ""test"": 2
   }
   )"
}
msgbox % test.myVar

Re: static continuation to object... is that documented?

Posted: 13 May 2022, 09:13
by RaptorX
teadrinker wrote:
12 May 2022, 14:46
You can use expression syntax:

Code: Select all

class test
{
   static myVar := "
   (Ltrim Join
   {
      ""info"": 1,
      ""test"": 2
   }
   )"
}
msgbox % test.myVar
Note that in the second code I was not using the static keyword.

So, if you dont use the static keyword you get an error, and if you use expression syntax you get a blank message box.

Code: Select all

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

now that @Helgef mentions static function vars, I think that is not the case these are Static/class variables, as documents says :
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.

Code: Select all

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?

Posted: 14 May 2022, 08:01
by Xeo786
lexikos wrote:
14 May 2022, 05:06
@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,

Code: Select all

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?

Posted: 16 May 2022, 10:03
by RaptorX
Helgef wrote:
14 May 2022, 00:42
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.
Splitting a Long Line into a Series of Shorter Ones

I never use it, it is weird.

Re: static continuation to object... is that documented?

Posted: 16 May 2022, 17:06
by RaptorX
Helgef wrote:
16 May 2022, 12:26
Method #2: [...] it can also be used with any command or expression.
Splitting a Long Line into a Series of Shorter Ones

I never use it, it is weird.
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?

Posted: 18 May 2022, 08:17
by RaptorX
lexikos wrote:
17 May 2022, 23:04
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?

Posted: 19 May 2022, 18:59
by lexikos
RaptorX wrote:
18 May 2022, 08:17
generally speaking in v1 the = operator is for strings,
See for example

Code: Select all

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.