简体   繁体   English

Autohotkey:有什么区别:=和=赋值运算符

[英]Autohotkey: What's the difference between := and = assignment operator

I don't quite understand what the difference is between := and = assignment operator in AutoHotKey. 我不太清楚AutoHotKey中的:==赋值运算符之间的区别。

On the manual, = is a traditional assignment, := is an expressional assignment. 在手册上, =是传统的作业, :=是表达作业。 I've never seen anyone use = operator, only := . 我从未见过有人使用=运算符,只有:=

Reference and image below 参考和图片如下

在此输入图像描述

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. 两种形式存在的原因是因为AutoHotKey的遗留语法和结构受到批处理语言(如MSDOS批处理文件和unix-shell脚本)的影响。 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): 这里使用新样式运算符进行备份并实现变量的默认值(使用old =运算符无法在一行中完成):

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. 但是,如果您需要实现更复杂的逻辑,则可以使用表达式语法更简洁地执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM