ligo/gitlab-pages/docs/language-basics/cheat-sheet.md
2019-06-11 03:37:12 +02:00

2.5 KiB

id title
cheat-sheet Cheat Sheet
Primitive Example
Strings "Tezos"
Characters "t"
Integers 42, 7
Natural numbers 42n, 7n
Unit unit
Boolean
const hasDriversLicense: bool = False;
const adult: bool = True;
Mutez (micro tez) 42mtz, 7mtz
Address "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx", "KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD"
Addition 3 + 4, 3n + 4n
Multiplication & Division 3 * 4, 3n * 4n, 10 / 5, 10n / 5n
Modulo 10 mod 3
Tuples
type name is (string * string);
const winner: name = ("John", "Doe");
const firstName: string = winner.0;
const lastName: string = winner.1;
Types type age is int, type name is string
Includes #include "library.ligo"
Functions (short form)
function add (const a : int ; const b : int) : int is
  block { skip } with a + b
Functions (long form)
function add (const a : int ; const b : int) : int is
  block {
    const result: int = a + b;
  } with result
Options
type middleName is option(string);
const middleName : middleName = Some("Foo");
const middleName : middleName = None;
Assignment const age: int = 5;
Assignment on an existing variable

⚠️ This feature is not supported at the top-level scope, you can use it e.g. within functions. Works for Records and Maps as well.
age := 18;, p.age := 21
Annotations ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)
Variants
type action is
| Increment of int
| Decrement of int
Variant (pattern) matching
const a: action = Increment(5);
case a of
| Increment n -> n + 1
| Decrement n -> n - 1
end
Records
type person is record
  age: int ;
  name: string ;
end

const john : person = record
  age = 18;
  name = "John Doe";
end

const name: string = john.name;
Maps
type prices is map(nat, tez);

const prices : prices = map
  10n -> 60mtz;
  50n -> 30mtz;
  100n -> 10mtz;
end

const price: option(tez) = prices[50n];