static __New() of any class definitionsAttempt to convert AHKv1 syntax to AHKv2 using the AHK-v2-script-converter project.
#NoEnv, SendMode Input, SetWorkingDir %A_ScriptDir%, and SetBatchLines, -1 are all defaults in v2.=) were removed.var = text to ✅var := "text"Sleep, 1000 to ✅Sleep 1000.MsgBox Hello to ✅MsgBox "Hello" or ✅MsgBox("Hello")Buffer() objects not VarSetCapacity(). Buffer is an Object, which always pass by reference, so you don't pass binary data by reference anymore. VarSetStrCapacity() exists but ONLY for string optimization.VarSetCapacity(var, capacity, fillByte) to ✅var := Buffer(capacity, fillByte)&) is no longer the "address of" operator.DllCall(…, "Ptr", &var) to ✅DllCall(…, "Ptr", var) (where var is a Buffer object)return (varToReturn, tmp := 1234) to ✅return (tmp := 1234, varToReturn)var := "Some ""quoted"" text" to ✅var := 'Some "quoted" text' or ✅var := "Some `"quoted`" text"unset (which is a state, not a value). Unset variables throw when read. Unset makes a great default parameter.MyFunc(param1, param2 := "") {…} to ✅MyFunc(param1, param2 := unset) {…}if (param2 != "") to ✅if IsSet(param2)var := [1, , 2] to ✅var := [1, unset, 2]FunctionCall(required1, required2, , optional2) to ✅FunctionCall(required1, required2, unset, optional2)var := "" ; Empty the variable to ✅var := Unset (unless you need var to be an empty string)New Stuff
for v in ["a", "b", "c"] MsgBox v
ComCall can iterate through a virtual function table without 3 DllCallsMyFunc(ByRef a) {…} to ✅MyFunc(&a) {…}result := MyFunc(a) to ✅result := MyFunc(&a)New Stuff
MsgBox as a function reference.execute(func) { func("a message") } execute(MsgBox)
In v1, you had to pass execute(Func("MsgBox")).
; This code: ;FAIL() { ; MsgBox "Function Failed" ;} ; Can become: FAIL() => MsgBox("Function failed") ; This code: ;SomeGlobalFunc(x) { ; x *= 5 ; MsgBox "Times 5: " x ; return x ;} ;FuncObj := Func("SomeGlobalFunc") ; Can become (no longer needing a global function): FuncObj := (x) => (x *= 5, MsgBox("Times 5: " x), x)
closureFactory(valueToBeEnclosed) { myClosure(newValue := unset) { return IsSet(newValue) ? valueToBeEnclosed := newValue : valueToBeEnclosed } return myClosure } myFuncObject := closureFactory(24) MsgBox myFuncObject() myFuncObject(25) MsgBox myFuncObject()
Map and Array. var := {"key": value} to ✅var := Map("key", value) (unless you are sure you want a basic Object not a Map)var := [] or ✅var := Array() (both work)object.LiteralName, Items by object[variableName]. data := Map("Count", 1234) MsgBox data.Count ; Access Property "Count" -> 1 MsgBox data["Count"] ; Access Item "Count" -> 1234
new to create a class anymore, you just call it by name.inst := new Class() to ✅inst := Class()ClassName) holds items that are static, the prototype (ClassName.Prototype) is copied to make new instances. In v1, the global object served both purposes.ClassName.Method() or ClassName.Property and instance methods can only be called instance.Method() or instance.Property. From inside a static method, this refers to the Global Object. From inside a normal method, this refers to the instance.static before its name. class Test { static Property := 1234 Property := 5768 static Method() { MsgBox "Static Method: " this.Property } Method() { MsgBox "Instance Method: " this.Property } } Test.Method() ; Static Method: 1234 Test().Method() ; Instance Method: 5678
static __New() of any class definitions