Chapter 2 : Types
Welcome onboard captain, I’m the ship’s mechanic. The first thing to do before departing is to define your ship’s parameters. Go ahead!
LIGO is a strongly and statically typed language. This means that the compiler checks how your contract processes data. If it passes the test, your contract will not fail at run-time due to inconsistent assumptions on your data. This is called type checking.
Built-in types
LIGO comes with all basic types built-in like string, int or tez for account balance or monetary transactions. You can find all built-in types on the LIGO gitlab.
Type aliases
Type aliasing consists in renaming a given type, when the context calls for a more precise name. This increases readability and maintainability of your smart contracts. For example we can choose to alias a string type as an animal breed – this will allow us to comunicate our intent with added clarity.
type breed is string
const dog_breed : breed = “Saluki”
Simple types
The type account_balances denotes a map from addresses to tez
type account_balances is map (address, tez)
const ledger : account_balances =
map [(“tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx” : address) -> 10mutez]
ℹ️We will look more deeply into the map construct in the following chapters.
Structured types
Often contracts require complex data structures, which in turn require well-typed storage or functions to work with. LIGO offers a simple way to compose simple types into structured types. The first of those structured types is the record, which aggregates types as fields and index them with a field name. In the example below we define an account type whick keeps the balance and number of previous transactions for a given account.
type account is record [
balance : tez;
transactions : nat
]
const my_account : account = record [
balance = 10mutez;
transactions = 5n
]
ℹ️We will look more deeply into the record construct in the following chapters.
Your mission
1- There is an online editor in the top right corner of this page. In the editor, define ship_code as a string type.
2- Then define the constant my_ship as a ship_code of value “020433”.
3- Then go ahead and validate your mission for a comparative view with the solution.
AWAITING VALIDATION
Type your solution above and validate your answer