VarSetCapacity() has VarName bug...

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pidgay
Posts: 16
Joined: 21 Aug 2017, 02:32

VarSetCapacity() has VarName bug...

21 Aug 2017, 02:58

VarSetCapacity() cannot hold capacity if 'VarName' belongs object.

Code: Select all

MsgBox % VarSetCapacity(vvv, 20)	; this prints '20'

oo := {}
MsgBox % VarSetCapacity(oo.x, 20)	; expected '20', but '0'
so I can't write like this.

Code: Select all

class Test {
	buffer := 0	
	__New(size)	{
		VarSetCapacity(this.buffer, size)		
	}
}
I have to write like this.

Code: Select all

outerBuffer := 0
class Test {
	__New(size, ByRef outerBuffer)	{
		VarSetCapacity(outerBuffer, size)		
	}
}
In documentation,
If VarName is not a valid variable name (such as a literal string or number), 0 is returned.

is this a bug, right?
fix this, please.

sorry for poor english
thanks for reading this
English is difficult...
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: VarSetCapacity() has VarName bug...

21 Aug 2017, 08:18

o.xx is not a variable name, its an expression which gets the content of the key xx in o. Use setCapacity for an object's fields. If you want to initialise the memory to zero, you can use eg, globalAlloc.

Cheers.
pidgay
Posts: 16
Joined: 21 Aug 2017, 02:32

Re: VarSetCapacity() has VarName bug...

21 Aug 2017, 09:16

Helgef wrote:o.xx is not a variable name, its an expression which gets the content of the key xx in o. Use setCapacity for an object's fields. If you want to initialise the memory to zero, you can use eg, globalAlloc.

Cheers.
I understood o.xx is an expression which gets the content of the key xx in o.
but field of class = key of object in Autohotkey????

Actually, I tend to use built-in functions(eg. VarSetCapacity) rather than DllCall(eg. GlobalAlloc)...
thank you for replying. ;)
English is difficult...
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: VarSetCapacity() has VarName bug...

21 Aug 2017, 11:13

Hello. I would describe a field as a key/value pair, you set the capacity for the value.

Cheers, :)
obeeb
Posts: 140
Joined: 20 Feb 2014, 19:15

Re: VarSetCapacity() has VarName bug...

21 Aug 2017, 20:02

pidgay wrote:but field of class = key of object in Autohotkey????
Autohotkey uses prototype based inheritance, everything is a key of object or a key of the base(prototype) object or the key of the base of the base object...
This includes constructors, destructors, properties, meta properties...
In Autohotkey "field of class" is just a key that is added in the auto generated __Init method, this method is called before the constructor(__New) when you use the new keyword.

A class in Autohotkey is not really a class but just syntactic sugar for the following:

Code: Select all

protoTest := {}
protoTest.__Class := "protoTest"
protoTest.__Init := Func("protoTest__Init")
protoTest.__New := Func("protoTest__New")
protoTest.print := Func("protoTest_print")

protoTest__Init(this) {
   this.var1 := 1
}
protoTest__New(this) {
   this.var2 := 2
}
protoTest_print(this) {
   MsgBox % "var1:" this.var1 " var2:" this.var2 " class:" this.base.__Class
}

; protoTest is now a "class" and we can create new instances of it 
t := new protoTest
t.print()
The above code is exactly the same as:

Code: Select all

class Test {
	var1 := 1
	__New()	{
      this.var2 := 2
	}
    
    print() {
      MsgBox % "var1:" this.var1 " var2:" this.var2 " class:" this.base.__Class
   }   
}

t := new Test
t.print()
If you are coming from languages with "real" classes that use class based inheritance like Java, c# or c++ this might seem strange but you will get used to it.
You should thoroughly read https://autohotkey.com/docs/Objects.htm#Custom_Objects it's probably the most difficult documentation page in Autohotkey but it's very well explained.
pidgay
Posts: 16
Joined: 21 Aug 2017, 02:32

Re: VarSetCapacity() has VarName bug...

09 Sep 2017, 01:50

You should thoroughly read https://autohotkey.com/docs/Objects.htm#Custom_Objects it's probably the most difficult documentation page in Autohotkey but it's very well explained.
Oh my god! What I wanna know was in document!! :shock:

Your kind answer makes me relieved of anxiety.

Thank you! x2 :D
English is difficult...

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aitaixy, Nerafius, RandomBoy and 203 guests