Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Class definition syntax for AutoHotkey


  • Please log in to reply
96 replies to this topic
maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011

If you try to use one as a callback, "this" will simply contain the first parameter of the callback - an integer.

:idea: (is this documented?)

If I understood you, Func("class.method") or RegisterCallback("class.method") should not be used as this behaviour could be changed?

[ot]Btw, I found a C sample app in the WinSDK that uses another method:
it stores the pointer to this with SetWindowLongPtr and the GWLP_USERDATA flag.[/ot]

Regards
maul.esel
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
Is it possible to use dynamic class names in the new operator?
This would be useful for a plugin architecture.

The following does not work:
Class test
{
	__New()
	{
		msgbox works
	}
}
name := "test"
t := new %name%()
It's possible to do it with
t := object("base", %name%)
t.__New()
This would actually be a reason to keep variable dereferencing in expressions.

  • Guests
  • Last active:
  • Joined: --
Can we change the Autohotkey's short name to Javahk or ahk# now?

IsNull
  • Moderators
  • 990 posts
  • Last active: May 15 2014 11:56 AM
  • Joined: 10 May 2007
I don't see any sense in dynamically instantiate classes. This just leads to strange code.
AHK is not able to lazyload code, so you can do a pluginsystem like that:

;Host.ahk ----------------------
registeredPlugins := {}
#include Plugins\*.ahk



;Plugin1.ahk ----------------------
class Plugin1
{

}
registeredPlugins.Insert(Plugin1)


;PluginSecond.ahk ----------------------
class PluginSecond
{

}
registeredPlugins.Insert(PluginSecond)

Then you have all base Plugin Types in the registeredPlugins List, where you can instantiate them in the host.ahk with:
pluginInstance := registeredPlugins[1].__New()

:D

@Guest: Can we deactivate the Guestpost right so that I can identify such boons? :-P

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009

I don't see any sense in dynamically instantiate classes. This just leads to strange code.
AHK is not able to lazyload code, so you can do a pluginsystem like that:

;Host.ahk ----------------------
registeredPlugins := {}
#include Plugins\*.ahk



;Plugin1.ahk ----------------------
class Plugin1
{

}
registeredPlugins.Insert(Plugin1)


;PluginSecond.ahk ----------------------
class PluginSecond
{

}
registeredPlugins.Insert(PluginSecond)

Then you have all base Plugin Types in the registeredPlugins List, where you can instantiate them in the host.ahk with:
pluginInstance := registeredPlugins[1].__New()

:D

@Guest: Can we deactivate the Guestpost right so that I can identify such boons? :-P


Right now it is necessary to include each plugin manually. I'm adding it to a list of plugins and create the objects automatically through this list.
This is still easier than doing it separately for each one, especially if there are further steps involved.

Crash&Burn
  • Members
  • 228 posts
  • Last active: Jul 16 2014 10:10 PM
  • Joined: 02 Aug 2009

I've decided to partially implement class var declarations in v1.1, to make defining static/class data easier. For example:

stk := new Stack
stk.Push("foo")
stk.Push("bar")
MsgBox % stk.Pop() stk.Pop()

; Without var declarations.
class Stack
{
    _StaticInit()
    {
        global Stack
        static _ := Stack._StaticInit()
        this.Push := Func("ObjInsert")
        this.Pop  := Func("ObjRemove")
    }
}

; With var declarations
class Stack
{
    var Push := Func("ObjInsert")
    var Pop  := Func("ObjRemove")
}

I'm confused over the usage and/or requirement for variable (var) assigned functions within a class structure. As I can't seem to call those functions that have been assigned inside a Class structure.
[AHK_L v1.1.00.00, Apr.30 2011, 64bit and 32bit.]

desk:=new DeskPad5(deskArr)

MsgBox, % "deskArr[1][1].title :: `n`n`t" deskArr[1][1].title
MsgBox, % "desk.tPad[1][1].class :: `n`n`t" desk.tPad[1][1].class
		;;
		;;
desk.Delete(2)	;; <---------- This does nothing
		;;
		;;	Delete is assigned below, in Class DeskPad5
		;;
desk.DeleteTest(1)
desk.AddWin(2)

MsgBox, % "desk.tPad[2][2].class :: `n`n`t" desk.tPad[2][2].winID

desk.DeleteTest(2)

MsgBox, % "desk.tPad[2][2].class :: `n`n`t" desk.tPad[2][2].winID
MsgBox, % "deskArr[2][2].class :: `n`n`t" deskArr[2][2].winID
pause


Class DeskPad5
{
	var baseWin:={winID: 0, title: "title", class: "class", x: 0, y: 0, w: 0, h: 0}
	var Delete:=Func("DeleteTest")

	__New(byRef testArr)
	{
		this.tPad:=Array()
		Loop, 9
		{
			this.tPad[A_Index] := {1: {base: this.baseWin}}
		}
		testArr:=this.tPad
		return this
	}

	AddWin(dx)
	{
		wx:=this.tPad[dx].MaxIndex()
		wx:=(wx ? wx + 1 : 1)
		this.tPad[dx] := {(wx): {base: this.baseWin}}
	}
	DeleteTest(dx)
	{
		this.tPad[dx] := {1: {base: this.baseWin}}
		MsgBox, DeleteTest (%dx%)
	}
}

Sorry, if this should be posted elsewhere. Feel free to split my post to Questions/Bugs/whatever...

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
You don't need to assign it if you declare the function inside the class object.