static continuation to object... is that documented? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
RaptorX
Posts: 371
Joined: 06 Dec 2014, 14:27
Contact:

static continuation to object... is that documented?

Post by RaptorX » 12 May 2022, 09:41

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.
Projects:
AHK-ToolKit

iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

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

Post by iseahound » 12 May 2022, 11:52

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).

User avatar
RaptorX
Posts: 371
Joined: 06 Dec 2014, 14:27
Contact:

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

Post by RaptorX » 12 May 2022, 13:36

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.
Projects:
AHK-ToolKit

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

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

Post by teadrinker » 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

User avatar
RaptorX
Posts: 371
Joined: 06 Dec 2014, 14:27
Contact:

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

Post by RaptorX » 13 May 2022, 09:13

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.
Projects:
AHK-ToolKit

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

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

Post by teadrinker » 13 May 2022, 09:31

I'm on phone now, can't test, but in this case myVar is an instance var, so you must use the keyword new.

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

Post by Helgef » 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.

User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

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

Post by Xeo786 » 14 May 2022, 01:41

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
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

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

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

Post by lexikos » 14 May 2022, 05:06

@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.

User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

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

Post by Xeo786 » 14 May 2022, 08:01

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"
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

User avatar
RaptorX
Posts: 371
Joined: 06 Dec 2014, 14:27
Contact:

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

Post by RaptorX » 16 May 2022, 10:03

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.
Projects:
AHK-ToolKit

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

Post by Helgef » 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.

User avatar
RaptorX
Posts: 371
Joined: 06 Dec 2014, 14:27
Contact:

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

Post by RaptorX » 16 May 2022, 17:06

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.
Projects:
AHK-ToolKit

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

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

Post by lexikos » 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.

User avatar
RaptorX
Posts: 371
Joined: 06 Dec 2014, 14:27
Contact:

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

Post by RaptorX » 18 May 2022, 08:17

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.
Projects:
AHK-ToolKit

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

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

Post by lexikos » 19 May 2022, 18:59

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.

Post Reply

Return to “Ask for Help (v1)”