3 ms·
They're identical to dictionaries/hash tables in pretty much any language, unless I'm missing something. Besides C, every language I've worked in comes with one
by oihoaihsfoiahsf 8y ago
They're identical to dictionaries/hash tables in pretty much any language, unless I'm missing something. Besides C, every language I've worked in comes with one of these things built-in. It's surprising to me that so many people consider this noteworthy today. Would someone please enlighten me as to why it is?
- sdegutis 8y agoIn both languages objects double as arrays, although they do this slightly differently. But the differences are mostly superficial.
- masklinn 8y agoPHP also does that, yet doesn't use its arrays for metaprogramming & object orientation (using a separate object system instead).
- dkrikun 8y agoone can "overload" operations on table instances, most importantly, accessors by forwarding them to another table, which is somewhat similar to prototype-based inheritance
- sdegutis 8y agoI don't think it's just similar, I think it actually is prototypical.
- nikki93 8y agoBecause `.foo` means `[‘foo’]` and because you can easily make sequences out of them (if you write `{ ... i1 = v1, v2, ... }` then `v2` automatically gets the “next” natural number as a key) the ergonomics make them usable as structures and arrays easily. Also the ‘:foo()` syntax binds the LHS of the operator as the first parameter for a method call, and metamethods allow you to easily implement inheritance / dispatch / etc. It’s more about ergonomics than availability.
- thanatropism 8y agoNot a full equivalence, but this[0] gets you x.foo, x.bar ergonomics as opposed to x['foo'] etc. [0] https://docs.python.org/3/library/types.html#types.SimpleNamespace https://docs.python.org/3/library/types.html#types.SimpleNam...
- majewsky 8y ago> `.foo` means `['foo']` Ruby does the same, except that it's `[:'foo']` because Ruby has two string types because fuck you.
- masklinn 8y ago> Would someone please enlighten me as to why it is? Both Lua and Javascript attach protocols to their hashmaps which most languages attach to other bits (classes, typeclasses, traits, …) — even if those other bits are underlaid by hashmaps at the end of the day. They're more like general-purpose objects which can act as hashmaps (badly in the case of javascript).
- kgwxd 8y ago>Besides C, every language I've worked in comes with one of these things built-in. JavaScript? When talking about a language most refer to as "JavaScript-like", it's definitely a noteworthy feature. Otherwise, you're right, it's pretty much expected to be available. Although, I'd say beginner developers might forget about it since the typical use case has string or number keys.