1
votes

I don't quite understand what the difference is between := and = assignment operator in AutoHotKey.

On the manual, = is a traditional assignment, := is an expressional assignment. I've never seen anyone use = operator, only:=.

Reference and image below

enter image description here

1

1 Answers

4
votes

The literal answer to your question is that := is followed by an expression and = is followed by a value; these are equivalent:

name = John Smith
name := "John Smith"

The reason both forms exist is because AutoHotKey's legacy syntax and structure was influenced by batch languages like MSDOS batch files and unix-shell scripts. These languages strive to be as human readable as possible because they get tinkered with a lot and generally don't the require the complex logic and structures you see in real programming languages.

Here's a script for performing a backup:

SOURCE = /home
DEST = /mnt/backup
run backup %SOURCE% %DEST%

The newer expression-based := operator is more flexible and powerful. However the syntax is relatively more verbose. Here the backup using the new style operator and implements default values for the variables (which cannot be done in a single line using the old = operator):

source  := source ? source : "/home"
dest    := dest ? dest : "/mnt/backup"
command := "backup " + source + " " + dest
run %command%

If all you're doing is assignment and execution, the batch file syntax is cleaner and less error prone. However, if you need to implement more complex logic, you can do so more concisely using the expression syntax.