guides:v1_v2_cheat_sheet

This is an old revision of the document!


v1 → c2 cheat sheet is a great idea. The purpose wouldn't be to cover eveything, only the important points. Off the top of my head,

1. Removing legacy assignment with `=`

2. Removing `%` force expression syntax

3. Functions are commands, and commands are functions. The fiest comma is removed.

4. Building from 3, you can do

  if (3 < 4)
      FAIL
  FAIL() {
      MsgBox "Function failed"
  }

5. Fat arrow syntax allows easy definition of functions

  FAIL() => MsgBox("Function failed")

6. v2 has proper support for memory buffers using `Buffer(size)`

7. Byref has been replaced by `&`.

8. ComCall can iterate through a virtual function table without 3 DllCalls

9. The auto execute section has been removed. Instead, any global code is run, including `static __new()`.

10. Gui has better syntax and uses an OOP approach.

11. Classes now have a `prototype` object.

12. You can't mix `static` and `instance` methods anymore. Static methods can only be called `Name_of_class.method()` and instance methods can only be called `instance.method()`. You can however bypass this change by calling `Name_of_class.prototype.method()`

13. You can't use `obj.1` or `obj.2` to access data. You have to use `obj[1]` and `obj[2]`.

14. This is because AHK v2 has better support for data structures. It incluses array() and map().

15. for loops can iterate values directly. In v1, the variable i will return 1, 2, 3… this is not a problem in v2 and will return a, b c.

  for i in ["a", "b", "c"]
      MsgBox %i%

16. v2 supports first class functions. So you can use `MsgBox` as a function reference. For example,

  execute(func) {
      func("a message")
  }
  execute(MsgBox)

In v1, you had to pass `execute(Func("MsgBox"))`

17. In multivalue return statements, v1 returns "a"

  return "a", "b", "c"

v2 would return "c".

18. You can use single quotes like `'this'`.

19. Hotkeys are now functions.

  s:: {
      MsgBox "hi"
      MsgBox "bye"
  }

You don't need a return anymore.

20. You no longer need the boilerplate on top of each v1 script.

  #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  ; #Warn  ; Enable warnings to assist with detecting common errors.
  SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

the above is now the default. Also `SetBatchLines -1` is default as well, so your script will run much faster.