1y
Rant about Javascript (Typescript) This is impressively dumb, I hate this language:
let m= new Map();
m.set({foo:2}, 23)
// Map { {…} → 23 }
m.set({foo:2}, 42)
// Map { {…} → 23, {…} → 42 }



Yes, I understand
why it is happening, that doesn’t change that it is fucking stupid!

How do I do this properly?

3
0
0
0
User avatar
pathetic piss kitten @kitten@elizabeth.cat
1y
Rant about Javascript (Typescript) @Fiona look I don't know what a Map with an object as a key is supposed to be

but I think I agree with it not working
2
0
0
0

User avatar
pathetic piss kitten @kitten@elizabeth.cat
1y
Rant about Javascript (Typescript) @Fiona conceptually speaking the value your trying to store would be directly inside the object, or... I guess stringify it before using it as key?
0
0
0
0
1y
Rant about Javascript (Typescript) @kitten Well, it is supposed to use the object as key. Kinda like the following (working!) C++:
#include <print>
#include <map>

struct foo {
	int bar;

	friend auto operator<=>(const foo& lhs, const foo& rhs) -> std::strong_ordering = default;
};

int main() {
	auto map = std::map<foo, int>{};
	map[foo{23}] = 42;
	map[foo{23}] = 1337;
	std::println("{}", map[foo{23}]);
}



(Yes, using a hashmap in C++ would be slightly more complicated because I’d have to define a hash-function for it, but still quite doable. The thing is: If C++ is much more convenient than a supposedly easy to use language, than that language has hugely failed; there really is no excuse here. Especially after the documentation for JS’s
Map advertizes everywhere that you can use objects as key types. Because that is clearly a lie in every practical sense of the word.

Then again, I guess the real takaway is that C++ just has the superior type system, being able to statically encode constness on an instance lever instead of requiring it to be defined on a per type basis.
(C++ doesn’t have two types for
Set and FrozenSet like python does, it just uses const set (or const unordered_set) when you use the set as key type in another set or map.)
It really shows the often underappreciated maturity in its design that is lacking from a lot of other languages:
There are a ton or problems in a large number of programming languages that have been comprehensively solved in C++ since before those other languages were even created.
0
0
0
0