Updated docs with blockless & anonymous functions, added variable mutation disclaimer, updated landing page pascaligo example.
This commit is contained in:
parent
63822e0430
commit
5ba34b3ac3
@ -36,7 +36,9 @@ Here's how you define a basic function that accepts two `ints` and returns a sin
|
||||
|
||||
```pascaligo
|
||||
function add(const a: int; const b: int): int is
|
||||
block { skip } with a + b
|
||||
begin
|
||||
const result: int = a + b;
|
||||
end with result;
|
||||
```
|
||||
|
||||
The function body consists of two parts:
|
||||
@ -44,9 +46,14 @@ The function body consists of two parts:
|
||||
- `block {<code>}` - logic of the function
|
||||
- `with <value>` - the return value of the function
|
||||
|
||||
> 💡 `skip` can be used as a placeholder for empty function blocks, when all the neccessary logic fits into `with` at the end. It is also possible to omit `block { skip } with`
|
||||
in the above example, leaving only `a + b`.
|
||||
#### Blockless functions
|
||||
|
||||
Functions that can contain all of their logic into a single instruction/expression, can be defined without the surrounding `block`.
|
||||
Instead, you can inline the necessary logic directly, like this:
|
||||
|
||||
```pascaligo
|
||||
function add(const a: int; const b: int): int is a + b
|
||||
```
|
||||
|
||||
<!--Cameligo-->
|
||||
|
||||
@ -65,3 +72,25 @@ value.
|
||||
|
||||
<!--END_DOCUSAURUS_CODE_TABS-->
|
||||
|
||||
<!--DOCUSAURUS_CODE_TABS-->
|
||||
<!--Pascaligo-->
|
||||
```pascaligo
|
||||
const increment : (int -> int) = (function (const i : int) : int is i + 1);
|
||||
// a = 2
|
||||
const a: int = increment(1);
|
||||
```
|
||||
<!--END_DOCUSAURUS_CODE_TABS-->
|
||||
|
||||
## Anonymous functions
|
||||
|
||||
Functions without a name, also known as anonymous functions are useful in cases when you want to pass the function as an argument or assign it to a key in a record/map.
|
||||
|
||||
Here's how to define an anonymous function assigned to a variable `increment`, with it's appropriate function type signature.
|
||||
<!--DOCUSAURUS_CODE_TABS-->
|
||||
<!--Pascaligo-->
|
||||
```pascaligo
|
||||
const increment : (int -> int) = (function (const i : int) : int is i + 1);
|
||||
// a = 2
|
||||
const a: int = increment(1);
|
||||
```
|
||||
<!--END_DOCUSAURUS_CODE_TABS-->
|
4
gitlab-pages/docs/language-basics/src/functions/add.ligo
Normal file
4
gitlab-pages/docs/language-basics/src/functions/add.ligo
Normal file
@ -0,0 +1,4 @@
|
||||
function add(const a: int; const b: int): int is
|
||||
begin
|
||||
const result: int = a + b;
|
||||
end with result;
|
@ -0,0 +1,3 @@
|
||||
const increment : (int -> int) = (function (const i : int) : int is i + 1);
|
||||
// a = 2
|
||||
const a: int = increment(1);
|
@ -0,0 +1 @@
|
||||
function add(const a: int; const b: int): int is a + b
|
@ -43,6 +43,9 @@ Variables, unlike constants, are mutable. They can't be used in a *global scope*
|
||||
|
||||
> 💡 Don't worry if you don't understand the function syntax yet. We'll get to it in upcoming sections of the docs.
|
||||
|
||||
> ⚠️ Please be wary that mutation only works within the function scope itself, values outside of the function scope will not be affected.
|
||||
|
||||
|
||||
|
||||
```pascaligo
|
||||
// won't work, use const for global values instead
|
||||
|
@ -18,55 +18,25 @@ const GridBlock = CompLibrary.GridBlock;
|
||||
const pre = "```";
|
||||
|
||||
const pascaligoExampleSmall = `${pre}pascaligo
|
||||
// variant defining pseudo multi-entrypoint
|
||||
// actions
|
||||
type action is
|
||||
| Increment of int
|
||||
| Decrement of int
|
||||
|
||||
function add
|
||||
(const a: int; const b: int): int is
|
||||
block { skip } with a + b
|
||||
|
||||
function subtract
|
||||
(const a: int; const b: int): int
|
||||
is block { skip } with a - b
|
||||
|
||||
// real entrypoint that re-routes the flow
|
||||
// based on the action provided
|
||||
function main
|
||||
(const p: action; const s: int):
|
||||
(list(operation) * int) is
|
||||
block { skip }
|
||||
with ((nil : list(operation)),
|
||||
case p of
|
||||
| Increment(n) -> add(s, n)
|
||||
| Decrement(n) -> subtract(s, n)
|
||||
end)
|
||||
${pre}`;
|
||||
|
||||
const pascaligoExample = `${pre}pascaligo
|
||||
// variant defining pseudo multi-entrypoint actions
|
||||
type action is
|
||||
| Increment of int
|
||||
| Decrement of int
|
||||
|
||||
function add (const a : int ; const b : int) : int is
|
||||
block { skip } with a + b
|
||||
function add (const a : int ; const b : int) : int is a + b
|
||||
|
||||
function subtract (const a : int ; const b : int) : int is
|
||||
block { skip } with a - b
|
||||
function subtract (const a : int ; const b : int) : int is a - b
|
||||
|
||||
// real entrypoint that re-routes the flow based
|
||||
// on the action provided
|
||||
function main (const p : action ; const s : int) :
|
||||
(list(operation) * int) is
|
||||
block { skip } with ((nil : list(operation)),
|
||||
// real entrypoint that re-routes the flow based on the action provided
|
||||
function main (const p : action ; const s : int) : (list(operation) * int) is
|
||||
((nil : list(operation)),
|
||||
case p of
|
||||
| Increment(n) -> add(s, n)
|
||||
| Decrement(n) -> subtract(s, n)
|
||||
| Increment (n) -> add (s, n)
|
||||
| Decrement (n) -> subtract (s, n)
|
||||
end)
|
||||
${pre}`;
|
||||
|
||||
const pascaligoExample = pascaligoExampleSmall;
|
||||
const cameligoExampleSmall = `${pre}ocaml
|
||||
type storage = int
|
||||
|
||||
|
@ -3,15 +3,13 @@ type action is
|
||||
| Increment of int
|
||||
| Decrement of int
|
||||
|
||||
function add (const a : int ; const b : int) : int is
|
||||
block { skip } with a + b
|
||||
function add (const a : int ; const b : int) : int is a + b
|
||||
|
||||
function subtract (const a : int ; const b : int) : int is
|
||||
block { skip } with a - b
|
||||
function subtract (const a : int ; const b : int) : int is a - b
|
||||
|
||||
// real entrypoint that re-routes the flow based on the action provided
|
||||
function main (const p : action ; const s : int) : (list(operation) * int) is
|
||||
block {skip} with ((nil : list(operation)),
|
||||
((nil : list(operation)),
|
||||
case p of
|
||||
| Increment (n) -> add (s, n)
|
||||
| Decrement (n) -> subtract (s, n)
|
||||
|
Loading…
Reference in New Issue
Block a user