Magic Constants
Autolang provides special compiler-resolved constants that change their value depending on where they are used. Unlike normal variables, these constants are replaced with their actual values at compile time, meaning they have zero runtime overhead.
They are incredibly useful for debugging, logging, and building assertion systems.
Available Constants
__LINE__Returns the current line number in the source file as an
Int.__FILE__Returns the name of the current file as a
String.__FUNC__Returns the name of the current function as a
String. If used outside a function, it may return an empty string or the global scope name.__CLASS__Returns the name of the current class as a
String. If used outside of any class, it returns an empty string"".
Example: Custom Logger
You can combine these constants to create a highly informative logging utility that tells you exactly where a message came from.
class Logger {
// We accept the context information as parameters
static func log(message: String, file: String, line: Int, className: String, funcName: String) {
println("[${file}:${line}] ${className}::${funcName} -> ${message}")
}
}
class NetworkManager {
func connect() {
// Evaluate the constants here, at the call site!
Logger.log("Attempting to connect...", __FILE__, __LINE__, __CLASS__, __FUNC__)
}
}
val net = NetworkManager()
net.connect()
// Output:
// [index.atl:11] NetworkManager::NetworkManager.connect() -> Attempting to connect...Example: Assertions
Magic constants perfectly pair with Autolang's built-in assert function from the standard library to catch bugs early in development.
func divide(a: Int, b: Int): Int {
// If b is 0, this will throw an Exception showing the exact file and line!
assert(b != 0, __FILE__, __LINE__)
return a / b
}
println(divide(10, 0))
// println(divide(10, 0)) // Exception: index.al:3: Wrong