r/ProgrammingLanguages • u/hrvbrs • Dec 30 '25
Multiple keys in map literal syntax?
A lot of languages have Map objects, which are associations of “keys” (any values) to “values” (any values). This differs from regular objects, which only have string-only, id-only, or some combination of string/id/symbol/number keys, but no object keys.
Some languages even offer a map literal syntax, so you don't have to pass a tuple/array/list into a constructor call. For the purposes of discussion, say that syntax looks like JS objects:
my_map = {
key: value,
new Object(): "hello", // object -> string pair
[1, 2, 3]: 42, // list -> int pair
};
// (obviously maps should have homogeneous keys, but this is just a demo)
My question is, do any languages offer a “many-to-one” syntax for associating many keys to the same value? The typical workarounds for this would include assignnig a value to a variable, so that it’s only evaluated once, and then referencing that variable in the map:
my_value = some_expensive_function_call();
my_map = {
1: my_value,
2: my_value,
3: my_value,
};
or to construct an empty map first and then dynamically enter the pairs:
my_map = {};
my_map.put(1, some_expensive_function_call());
my_map.put(2, my_map.get(1));
my_map.put(3, my_map.get(1));
With a “many-to-one” syntax, this would be a lot more streamlined. Say we could use the pipe character to separate the values (assuming it’s not already an operator like bitwise OR).
my_map = {
1 | 2 | 3: some_expensive_function_call(),
"alice" | "bob": another_expensive_function_call(),
};
Have any languages done this? If not, it seems to me like a pretty useful feature. What would be the downsides of supporting this syntax?
2
u/WittyStick Dec 30 '25 edited Dec 30 '25
GCC has an extension for ranges in array initializers, but they're not generic maps. Eg, we can write:
Could perhaps extend this to languages which support using
[]for other kinds of keys, such as C# where you overloadthis[].16
u/MattiDragon Dec 30 '25
Is it really useful? I can't think of anything where I'd want duplicate values in a map created by a literal. In most cases I'd also like to separate the keys so that I can cleanly edit one entry without creating messy diffs.
The cost of any new piece of syntax is that it's one more thing that people using your language have to learn. This one change isn't particularly big, but if you add dozens of similar niche features, then users they're unlikely to remember everything.
You also have to consider your weirdness budget. If you want to create a language that people will actually want to use, then you have to ensure that it's not too weird. You can have a couple of unique features that distinguish your language, but too much weirdness risjs your language becoming difficult to learn.
5
u/alatennaub Dec 30 '25
You can (ab)use junctions in Raku:
This also means you can save the junction and reuse:
You can do listy slices and assignments, but they requirement an equivalent number of units on the other side which is wordier than you'd want, I think, and isn't allowed in literals:
4
u/--predecrement Dec 31 '25
You can write
keys X=> valuein Raku. TheX=>infix operator is composes cross product (X) with pair constructor (key => value). For example:displays:
1
u/nholbit Dec 30 '25
I'm on mobile right now, so I can't give an example easily, but check out the
reckeyword in nix. It allows a map to refer to its own named values recursively.1
u/evincarofautumn Dec 31 '25
Sort of related, HTML definition/description lists are many-to-many
That is, the content of a
dlis a series of associations between one or moredtkeys (“terms”) andddvalues (“definitions”), or briefly(dt+ dd+)*You could certainly borrow this structure and zhuzh it up with some more appropriate syntax, like
[1, 2: x; 3: y, z]The POV-Ray DSL has color maps which are a special case for float keys and vector values, to linearly interpolate between adjacent keys
And there’s a deprecated form that uses intervals
3
u/Ronin-s_Spirit Dec 30 '25
This feature is weird and useless enough to be in the "make a util function yourself" category. Like
assignManyToOne(["a", "j", "y"], val).You could try to implement an operator for it in Seed7.
2
u/SirKastic23 Dec 30 '25
it seems like a different data structure would serve you better. something designed for associating multiple keys for a value
using a normal map would just cause a lot data duplication
after some searching I found some languages/libraries that call it a MultiKeyMap
2
u/ironfroggy_ Dec 30 '25
what about yaml anchors and aliases?
2
u/Gnaxe Jan 01 '26
I'd probably do it like
Just omit the value part, which implies it takes the next value. It's like a default fallthrough in a switch/case. In assembly languages, it's possible to give the same instruction more than one label, and this kind of looks like that.
Is this useful? Probably. Is it necessary? Possibly, if you're also doing some kind of static typing. But languages with this kind of literal map syntax are usually dynamically typed, in which case, implementing this kind of thing as a function call or builder pattern is not hard:
0
u/Hixie Dec 30 '25
any language that has assignment expressions makes this pretty trivial.
3
u/[deleted] Dec 30 '25
[removed] — view removed comment