--- id: maps-records title: Maps, Records --- So far we've seen pretty basic data types. LIGO also offers more complex built-in constructs, such as Maps and Records. ## Maps Maps are natively available in Michelson, and LIGO builds on top of them. A requirement for a Map is that its keys be of the same type, and that type must be comparable. Here's how a custom map type is defined: ```pascaligo type ledger is map(address, tez); ``` ```cameligo type ledger = (address, tez) map ``` And here's how a map value is populated: ```pascaligo const ledger: ledger = map ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address) -> 1000mutez; ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) -> 2000mutez; end ``` > Notice the `->` between the key and its value and `;` to separate individual map entries. > > `("": address)` means that we type-cast a string into an address. ```cameligo let ledger: ledger = Map.literal [ (("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address), 1000mutez) ; (("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), 2000mutez) ; ] ``` > Map.literal constructs the map from a list of key-value pair tuples, `(, )`. > Note also the `;` to separate individual map entries. > > `("": address)` means that we type-cast a string into an address. ### Accessing map values by key If we want to access a balance from our ledger above, we can use the `[]` operator/accessor to read the associated `tez` value. However, the value we'll get will be wrapped as an optional; in our case `option(tez)`. Here's an example: ```pascaligo const balance: option(tez) = ledger[("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address)]; ``` ```cameligo let balance: tez option = Map.find_opt ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) ledger ``` #### Obtaining a map value forcefully Accessing a value in a map yields an option, however you can also get the value directly: ```pascaligo const balance: tez = get_force(("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address), ledger); ``` ```cameligo let balance: tez = Map.find ("tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN": address) ledger ``` ## Records Records are a construct introduced in LIGO, and are not natively available in Michelson. The LIGO compiler translates records into Michelson `Pairs`. Here's how a custom record type is defined: ```pascaligo type user is record id: nat; is_admin: bool; name: string; end ``` ```cameligo type user = { id: nat; is_admin: bool; name: string; } ``` And here's how a record value is populated: ```pascaligo const user: user = record id = 1n; is_admin = True; name = "Alice"; end ``` ```cameligo let user: user = { id = 1n; is_admin = true; name = "Alice"; } ``` ### Accessing record keys by name If we want to obtain a value from a record for a given key, we can do the following: ```pascaligo const is_admin: bool = user.is_admin; ``` ```cameligo let is_admin: bool = user.is_admin ```