Logo

Programming-Idioms

  • Lua

Idiom #282 Use a custom type as map key

Declare a type Foo, and create a new map with Foo as key type.

Mention the conditions on Foo required to make it a possible map key type.

local map={[Foo]=true}

There is no custom type in lua, anything except nil could be a key in map.
class Foo{};
Map<Foo, int> m = {};

note that typedef can also be used to declare a type, ex: typedef Foo = List<int>;

New implementation...
< >
programming-idioms.org