Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a constant in AutoHotkey?

Many languages have methods for declaring constant values that can't be changed once created.

public static final int MAX_TIME = 25; // Java
const int MAX_TIME = 25;               // C++
const MAX_TIME = 25;                   // JavaScript

Is there an equivalent way to declare constants in AutoHotkey?

like image 236
Stevoisiak Avatar asked Oct 21 '25 18:10

Stevoisiak


1 Answers

One solution is to use a function.

MAX_TIME() {
    return 25
}
MsgBox % MAX_TIME()

Attempting to redefine the function produces Error: Duplicate function definition.

MAX_TIME() {
    return 25
}
MsgBox % MAX_TIME()
MAX_TIME() {
    return 22
}

Note that even after the function has been defined, creating a value with MAX_TIME = 20 is still allowed, so be consistent and always use the function instead of a variable.

Two other approaches are described at https://autohotkey.com/board/topic/90774-is-it-possible-to-create-constant/. The first is to simply use a variable and remember not to change it. This seems to be the preferred approach.

The second is to use a class property to store the constant, and override __Set() so that the value cannot be changed.

Python works pretty much the same way. See How do I create a constant in Python?

like image 81
Jim K Avatar answered Oct 24 '25 13:10

Jim K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!