@buliasz
lexikos wrote: ↑19 Aug 2019, 03:30
One problem is that
[] creates an empty
Array and
{} creates an empty Object, so overloading based on whether there are key-value pairs vs. just values would not completely work.
The second case overlaps with function definitions. Function definition expressions, where full functions are defined and referenced within an expression, are not supported yet but are planned.
The use of
: for pairing parameters of
{} (which is just
Object() in disguise) is trivial to extend to other functions, as in
Map("key": "value") or
DllCall("IsWindow", ptr: aHwnd), but it's a bit odd to have special syntax for specific functions. It also overlaps with potential support for named parameters in general, which could potentially be utilized by DllCall in the same manner, but maybe not Map, because...
lexikos wrote: ↑19 Aug 2019, 03:30
It is unlikely that any "shorthand" syntax I add for Map will allow quote marks to be omitted. Object notation allows it because they are "properties", and the usual syntax for accessing them does not use quote marks. (Now it makes more sense to
require the quote marks to be omitted for properties.) Map, on the other hand,
always requires quote marks around literal keys.
One new idea is to have a separate function calling syntax specifically for a list of pairs, where each item is merely a parameter, but must be specified in pairs. For example,
Map{"key": "value"}.
An older idea...
myCol {Prop: 4, [1]: "first"} could be translated to
(myCol.Prop := 4, myCol[1] := "first", myCol). That is, it could be shorthand for changing multiple properties/elements, and would naturally also work for
Collection.new() {...}. However, it might be difficult to parse in cases like
getObject() {Prop: "to set"} which look like function definitions.
Source: Object.ahk/Class.md at master · Lexikos/Object.ahk
A slight change to reduce ambiguity:
myCol.{Prop: 4, [1]: "first"}. However, it would probably be clearer as
myCol.{Prop := 4, [1] := "first"}. It could be extended to
obj1.{prop, [index]} := obj2 to copy
obj2.prop and
obj2[index] to
obj1. This is all theoretical and likely to complicated for the current parser.
Some other options I've considered:
- {{ ... }}, which isn't otherwise valid (or ambiguous) because the outer {} has only one item.
- #{ ... }, which has the (misleading) connotation that it is creating a "hash" (technically, hash table, which Map is currently not).
With something like
Metalua, but for AutoHotkey, we could easily trial syntax ideas...
But I still think dedicated syntax for Map is unnecessary.
If you want to make a clear distinction between keys and values, you can always use a normal assignment.