ligo/gitlab-pages/docs/language-basics/variables-and-constants.md

143 lines
3.3 KiB
Markdown
Raw Normal View History

---
id: constants-and-variables
title: Constants & Variables
---
import Syntax from '@theme/Syntax';
2020-02-05 19:28:40 +04:00
The next building block after types are *constants* and *variables*.
## Constants
2020-02-05 19:28:40 +04:00
Constants are immutable by design, which means their values cannot be
reassigned. Put in another way, they can be assigned once, at their
declaration. When defining a constant you need to provide a `name`,
`type` and a `value`:
<Syntax syntax="pascaligo">
2020-02-05 19:28:40 +04:00
```pascaligo group=a
const age : int = 25
```
2020-02-05 19:28:40 +04:00
You can evaluate the constant definition above using the following CLI
command:
```shell
2020-02-05 19:28:40 +04:00
ligo evaluate-value gitlab-pages/docs/language-basics/src/variables-and-constants/const.ligo age
# Outputs: 25
```
</Syntax>
<Syntax syntax="cameligo">
2020-02-05 19:28:40 +04:00
```cameligo group=a
let age : int = 25
```
2020-02-05 19:28:40 +04:00
You can evaluate the constant definition above using the following CLI
command:
```shell
2020-02-05 19:28:40 +04:00
ligo evaluate-value gitlab-pages/docs/language-basics/src/variables-and-constants/const.mligo age
# Outputs: 25
```
</Syntax>
<Syntax syntax="reasonligo">
2020-02-05 19:28:40 +04:00
```reasonligo group=a
let age : int = 25;
2019-12-10 17:47:31 +04:00
```
2020-02-05 19:28:40 +04:00
You can evaluate the constant definition above using the following CLI
command:
2019-12-10 17:47:31 +04:00
```shell
2020-02-05 19:28:40 +04:00
ligo evaluate-value gitlab-pages/docs/language-basics/src/variables-and-constants/const.religo age
2019-12-10 17:47:31 +04:00
# Outputs: 25
```
</Syntax>
## Variables
<Syntax syntax="pascaligo">
2020-02-10 22:07:20 +04:00
Variables, unlike constants, are *mutable*. They cannot be declared in
a *global scope*, but they can be declared and used within functions,
or as function parameters.
2020-02-05 19:28:40 +04:00
> ⚠️ Please be wary that mutation only works within the function scope
> itself, values outside of the function scope will not be
> affected. In other words, when a function is called, its arguments
> are copied, *as well as the environment*. Any side-effect to that
> environment is therefore lost when the function returns.
2020-02-05 19:28:40 +04:00
```pascaligo group=b
// The following is invalid: use `const` for global values instead.
2020-02-10 22:07:20 +04:00
// var four : int := 4
2020-02-05 19:28:40 +04:00
function add (const a : int; const b : int) : int is
block {
2020-02-12 01:29:12 +04:00
var c : int := a + 2*b;
c := c - b
2020-02-05 19:28:40 +04:00
} with c
```
2020-02-05 19:28:40 +04:00
> ⚠️ Notice the assignment operator `:=` for `var`, instead of `=` for
> constants.
2020-02-05 19:28:40 +04:00
You can run the `add` function defined above using the LIGO compiler
like this:
```shell
2020-02-05 19:28:40 +04:00
ligo run-function gitlab-pages/docs/language-basics/src/variables-and-constants/add.ligo add '(1,1)'
# Outputs: 2
```
</Syntax>
<Syntax syntax="cameligo">
2020-02-05 19:28:40 +04:00
As expected in the pure subset of a functional language, CameLIGO only
2020-02-10 22:07:20 +04:00
features *constant values*: once they are declared, the value cannot
be changed (or "mutated").
2020-02-05 19:28:40 +04:00
```cameligo group=c
let add (a : int) (b : int) : int =
let c : int = a + b in c
```
2020-02-05 19:28:40 +04:00
You can run the `add` function defined above using the LIGO compiler
like this:
```shell
2020-02-05 19:28:40 +04:00
ligo run-function gitlab-pages/docs/language-basics/src/variables-and-constants/add.mligo add '(1,1)'
# Outputs: 2
```
</Syntax>
<Syntax syntax="reasonligo">
2019-12-10 17:47:31 +04:00
2020-02-05 19:28:40 +04:00
As expected in the pure subset of a functional language, ReasonLIGO
2020-02-10 22:07:20 +04:00
only features *constant values*: once they are declared, the value
2020-02-05 19:28:40 +04:00
cannot be changed (or "mutated").
2019-12-10 17:47:31 +04:00
2020-02-05 19:28:40 +04:00
```reasonligo group=c
let add = ((a, b): (int, int)): int => {
let c : int = a + b;
2019-12-10 17:47:31 +04:00
c;
};
```
2020-02-05 19:28:40 +04:00
You can run the `add` function defined above using the LIGO compiler
like this:
2019-12-10 17:47:31 +04:00
```shell
2020-02-05 19:28:40 +04:00
ligo run-function gitlab-pages/docs/language-basics/src/variables-and-constants/add.religo add '(1,1)'
2019-12-10 17:47:31 +04:00
# Outputs: 2
```
</Syntax>