New Shorthand Syntax for Arrays

June 16, 2025

Onetastic has an update with a Macro language improvement that will make it easier to manage arrays. Previously arrays could be created with Array function or using the bracket operator. Array function didn't allow specifying array keys. It only allowed specifying values. If you wanted to specify keys as well, you could use Array_FromKeysAndValues function, which is difficult to use because keys and values are separated:

$keys = Array("color", "size", "type") $values = Array("red", "large", "t-shirt") $product = Array_FromKeysAndValues($keys, $values) // This created the following array // "color" => "red" // "size" => "large" // "type" => "t-shirt" // Alternatively you could do this: $product["color"] = "red" $product["size"] = "large" $product["type"] = "t-shirt"

With the latest update you can either use Array function or the new shorthand bracket ([]) and arrow (=>) operators to specify both keys and values:

// New Array function syntax with arrow operator (=>) $product = Array("color" => "red", "size" => "large", "type" => "t-shirt") // Or even easier bracket syntax $product = ["color" => "red", "size" => "large", "type" => "t-shirt"]

This makes it much easier to use functions that take arrays with specific keys:

// Show the file picker to pick an existing file // Allow text or image files to be picked $types["Text Files"] = "*.txt" $types["Image Files"] = "*.jpg;*.png;*.gif" // Show the file picker $files = FileSystem_ShowOpenFileDialog(false, $types) // This can now be written as $files = FileSystem_ShowOpenFileDialog(false, ["Text Files" => "*.txt", "Image Files" => "*.jpg;*.png;*.gif"])

When creating arrays with standard numeric indices, the keys can be omitted as before.

Looping over arrays with access to keys

Another improvement along with this is the ability to use foreach to have access to both keys and values of an array. This is also achieved using the arrow operator (=>):
$message = "Product Info:n" foreach ($key => $value in $product) $message &= $key & ": " & $value & "n" ShowMessage($message) // This will show the following message // Product Info: // color: red // size: large // type: t-shirt

Comments

Name
Comment

Other Posts

Show all posts