ligo/gitlab-pages/docs/reference/toplevel.md
2020-06-08 11:58:23 +02:00

4.1 KiB

id title description hide_table_of_contents
toplevel Top-Level Available functions at the top level true

import Syntax from '@theme/Syntax'; import SyntaxTitle from '@theme/SyntaxTitle';

These functions are available without any needed prefix.

function is_nat: int -> option(nat) val is_nat: int -> nat option let is_nat: int => option(nat)

Convert an int to a nat if possible.

Note that Michelson.is_nat is deprecated. Please use is_nat instead.

function abs: int -> nat val abs: int -> nat let abs: int => nat

Cast an int to nat.

function int: nat -> int val int: nat -> int let int: nat => int

Cast an nat to int.

const unit: unit val unit: unit let (): unit

A helper to create a unit.

function failwith : 'a -> unit val failwith : 'a -> unit let failwith: 'a => unit

Cause the contract to fail with an error message or integer. Other types are not supported at the moment.

Using this currently requires in general a type annotation on the failwith call.

function main (const p : int; const s : unit) : list (operation) * unit is
  block {
    if p > 10 then failwith ("Failure.") else skip
  }
  with ((nil : list (operation)), s)
let main (p,s : int * unit) = if p > 10 then failwith "Failure."
let main = ((p,s) : (int, unit)) =>
  if (p > 10) { failwith ("Failure."); };
`Current.failwith` is deprecated. Use `Tezos.failwith` or `failwith` instead. `Current.failwith` is deprecated. Use `Tezos.failwith` or `failwith` instead. function assert : bool -> unit val assert : bool -> unit let assert: bool => unit

Check if a certain condition has been met. If not the contract will fail.

function ediv : int -> int -> option (int * nat) function ediv : mutez -> nat -> option (mutez * mutez) function ediv : mutez -> mutez -> option (nat * mutez) function ediv : nat -> nat -> option (nat * nat) val ediv : int -> int -> (int * nat) option val ediv : mutez -> nat -> (mutez * mutez) option val ediv : mutez -> mutez -> (nat * mutez) option val ediv : nat -> nat -> (nat * nat) option let ediv: (int, int) => option((int, nat)) let ediv: (mutez, nat) => option((mutez, mutez)) let ediv: (mutez, mutez) => option((nat, mutez)) let ediv: (nat, nat) => option((nat, nat))

Compiles to Michelson EDIV, one operation to get both the quotient and remainder of a division. ediv x y returns None if y is zero, otherwise returns Some (quotient, remainder) such that x = (quotient * y) + remainder and 0 <= remainder < abs(y).