ligo/gitlab-pages/docs/reference/toplevel.md

166 lines
4.1 KiB
Markdown
Raw Normal View History

2020-04-01 21:00:39 +04:00
---
id: toplevel
title: Top-Level
2020-04-01 21:00:39 +04:00
description: Available functions at the top level
hide_table_of_contents: true
---
import Syntax from '@theme/Syntax';
import SyntaxTitle from '@theme/SyntaxTitle';
These functions are available without any needed prefix.
<SyntaxTitle syntax="pascaligo">
function is_nat: int -> option(nat)
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-03 12:01:35 +04:00
<SyntaxTitle syntax="cameligo">
2020-04-01 21:00:39 +04:00
val is_nat: int -> nat option
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-03 12:01:35 +04:00
<SyntaxTitle syntax="reasonligo">
2020-04-01 21:00:39 +04:00
let is_nat: int => option(nat)
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-01 21:00:39 +04:00
Convert an `int` to a `nat` if possible.
2020-04-03 16:51:16 +04:00
Note that `Michelson.is_nat` is deprecated. Please use `is_nat` instead.
2020-04-01 21:00:39 +04:00
<SyntaxTitle syntax="pascaligo">
function abs: int -> nat
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-03 12:01:35 +04:00
<SyntaxTitle syntax="cameligo">
2020-04-01 21:00:39 +04:00
val abs: int -> nat
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-03 12:01:35 +04:00
<SyntaxTitle syntax="reasonligo">
2020-04-01 21:00:39 +04:00
let abs: int => nat
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-01 21:00:39 +04:00
Cast an `int` to `nat`.
<SyntaxTitle syntax="pascaligo">
function int: nat -> int
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-03 12:01:35 +04:00
<SyntaxTitle syntax="cameligo">
2020-04-01 21:00:39 +04:00
val int: nat -> int
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-03 12:01:35 +04:00
<SyntaxTitle syntax="reasonligo">
2020-04-01 21:00:39 +04:00
let int: nat => int
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-01 21:00:39 +04:00
Cast an `nat` to `int`.
<SyntaxTitle syntax="pascaligo">
const unit: unit
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-03 12:01:35 +04:00
<SyntaxTitle syntax="cameligo">
2020-04-01 21:00:39 +04:00
val unit: unit
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-03 12:01:35 +04:00
<SyntaxTitle syntax="reasonligo">
2020-04-01 21:00:39 +04:00
let (): unit
2020-04-02 17:14:07 +04:00
</SyntaxTitle>
2020-04-01 21:00:39 +04:00
A helper to create a unit.
2020-06-08 13:58:23 +04:00
<a name="failwith"></a>
2020-04-01 21:00:39 +04:00
<SyntaxTitle syntax="pascaligo">
2020-06-08 13:58:23 +04:00
function failwith : 'a -> unit
2020-04-01 21:00:39 +04:00
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
2020-06-08 13:58:23 +04:00
val failwith : 'a -> unit
2020-04-01 21:00:39 +04:00
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
2020-06-08 13:58:23 +04:00
let failwith: 'a => unit
2020-04-01 21:00:39 +04:00
</SyntaxTitle>
2020-06-08 13:58:23 +04:00
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.
<Syntax syntax="pascaligo">
```pascaligo
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)
```
</Syntax>
<Syntax syntax="cameligo">
```cameligo
let main (p,s : int * unit) = if p > 10 then failwith "Failure."
```
</Syntax>
<Syntax syntax="reasonligo">
```reasonligo
let main = ((p,s) : (int, unit)) =>
if (p > 10) { failwith ("Failure."); };
```
</Syntax>
<Syntax syntax="cameligo">
`Current.failwith` is deprecated. Use `Tezos.failwith` or `failwith` instead.
</Syntax>
<Syntax syntax="reasonligo">
`Current.failwith` is deprecated. Use `Tezos.failwith` or `failwith` instead.
</Syntax>
2020-04-01 21:00:39 +04:00
<SyntaxTitle syntax="pascaligo">
function assert : bool -> unit
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
2020-04-03 12:01:35 +04:00
val assert : bool -> unit
2020-04-01 21:00:39 +04:00
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
2020-04-07 21:27:23 +04:00
let assert: bool => unit
2020-04-01 21:00:39 +04:00
</SyntaxTitle>
Check if a certain condition has been met. If not the contract will fail.
2020-04-07 21:27:23 +04:00
<SyntaxTitle syntax="pascaligo">
function ediv : int -> int -> option (int * nat)
</SyntaxTitle>
<SyntaxTitle syntax="pascaligo">
function ediv : mutez -> nat -> option (mutez * mutez)
</SyntaxTitle>
<SyntaxTitle syntax="pascaligo">
2020-04-08 02:11:19 +04:00
function ediv : mutez -> mutez -> option (nat * mutez)
2020-04-07 21:27:23 +04:00
</SyntaxTitle>
<SyntaxTitle syntax="pascaligo">
function ediv : nat -> nat -> option (nat * nat)
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val ediv : int -> int -> (int * nat) option
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val ediv : mutez -> nat -> (mutez * mutez) option
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
2020-04-08 02:11:19 +04:00
val ediv : mutez -> mutez -> (nat * mutez) option
2020-04-07 21:27:23 +04:00
</SyntaxTitle>
<SyntaxTitle syntax="cameligo">
val ediv : nat -> nat -> (nat * nat) option
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let ediv: (int, int) => option((int, nat))
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let ediv: (mutez, nat) => option((mutez, mutez))
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
2020-04-08 02:11:19 +04:00
let ediv: (mutez, mutez) => option((nat, mutez))
2020-04-07 21:27:23 +04:00
</SyntaxTitle>
<SyntaxTitle syntax="reasonligo">
let ediv: (nat, nat) => option((nat, nat))
</SyntaxTitle>
2020-04-08 02:33:20 +04:00
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)`.