Skip to content

Commit

Permalink
[book] Minor edits (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka authored May 17, 2024
1 parent b68670d commit 90b7388
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 34 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Run tests for Move packages

# Set the VERSION variable to use when downloading the binary
# Currently, requires explicit setup, ideally it should be fetched
# automatically
# env:
# VERSION: 'v1.24.1'

on:
pull_request:

jobs:
download-binary:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Download Sui
uses: jaxxstorm/[email protected]
with:
repo: MystenLabs/sui
platform: ubuntu
rename-to: sui-archive
cache: enable
extension: '.tgz'
# The code below is unpacking the .tgz because the action used above does
# not recognize the extension. The PR to the action repo is open and once
# it's in (and if) we'll be able to remove this.
- run: 'ls /opt/hostedtoolcache/'
- name: Unpack the archive (.tgz is not supported currently)
run: 'tar -xvzf /opt/hostedtoolcache/MystenLabs/sui/latest/ubuntu-x64/sui-archive'
- name: Move binaries to $PATH
run: mv sui* /opt/hostedtoolcache/MystenLabs/sui/latest/ubuntu-x64
- run: chmod 0755 /opt/hostedtoolcache/MystenLabs/sui/latest/ubuntu-x64/sui

# Run the tests in every directory using the latest mainnet binary
- run: 'sui move test --path packages/samples'
- run: 'sui move test --path packages/reference'
- run: 'sui move test --path packages/todo_list'
- run: 'sui move test --path packages/hello_world'
2 changes: 1 addition & 1 deletion book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
- [String](./move-basics/string.md)
- [Control Flow](./move-basics/control-flow.md)
- [Constants](./move-basics/constants.md)
- [Assert and Abort](./move-basics/assert-and-abort.md)
- [Aborting Execution](./move-basics/assert-and-abort.md)
- [Function](./move-basics/function.md)
- [Struct Methods](./move-basics/struct-methods.md)
- [Visibility Modifiers](./move-basics/visibility.md)
Expand Down
23 changes: 12 additions & 11 deletions book/src/move-basics/assert-and-abort.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Assert and Abort
# Aborting Execution

<!-- Consider "aborting execution" -->

Expand All @@ -21,18 +21,18 @@ Links:
-->

A transaction can either succeed or fail. Successful execution applies all the changes made to
objects and on-chain data, and the transaction is committed to the blockchain. If a transaction
aborts, and changes are not applied. The `abort` keyword is used to abort a transaction and revert
the changes made so far.
objects and on-chain data, and the transaction is committed to the blockchain. Alternatively, if a
transaction aborts, the changes are not applied. The `abort` keyword is used to abort a transaction
and revert the changes made so far.

> It is important to note that there is no catch mechanism in Move. If a transaction aborts, the
> changes made so far are reverted, and the transaction is considered failed.
## Abort

The `abort` keyword is used to abort the execution of a transaction. It is used in combination with
an abort code, which will be returned to the caller of the transaction. The abort code is an integer
of type `u64` and can be any value.
an abort code, which will be returned to the caller of the transaction. The abort code is an
[integer](./primitive-types.md) of type `u64`.

```move
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:abort}}
Expand All @@ -45,18 +45,19 @@ The code above will, of course, abort with abort code `1`.
The `assert!` macro is a built-in macro that can be used to assert a condition. If the condition is
false, the transaction will abort with the given abort code. The `assert!` macro is a convenient way
to abort a transaction if a condition is not met. The macro shortens the code otherwise written with
an `if` expression + `abort`.
an `if` expression + `abort`. The `code` argument is required and has to be a `u64` value.

```move
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:assert}}
```

## Error constants

To make error codes more descriptive, it is a good practice to define error constants. Error
constants are defined as `const` declarations and are usually prefixed with `E` followed by a camel
case name. Error constants are no different from other constants and don't have special handling. So
their addition is purely a practice for better code readability.
To make error codes more descriptive, it is a good practice to define
[error constants](./constants.md). Error constants are defined as `const` declarations and are
usually prefixed with `E` followed by a camel case name. Error constants are no different from other
constants and don't have special handling, however, they are used to increase the readability of the
code and make it easier to understand the abort scenarios.

```move
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:error_const}}
Expand Down
32 changes: 12 additions & 20 deletions book/src/move-basics/primitive-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ Where:
- `<expression>` - the value to be assigned to the variable

```move
let x: bool = true;
let mut y: u8 = 42;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:variables_and_assignment}}
```

A mutable variable can be reassigned using the `=` operator.
Expand All @@ -42,8 +41,7 @@ y = 43;
Variables can also be shadowed by re-declaring.

```move
let x: u8 = 42;
let x: u8 = 43;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:shadowing}}
```

## Booleans
Expand All @@ -53,8 +51,7 @@ The `bool` type represents a boolean value - yes or no, true or false. It has tw
the type - the compiler can infer it from the value.

```move
let x = true;
let y = false;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:boolean}}
```

Booleans are often used to store flags and to control the flow of the program. Please, refer to the
Expand All @@ -72,10 +69,7 @@ Move supports unsigned integers of various sizes: from 8-bit to 256-bit. The int
- `u256` - 256-bit

```move
let x: u8 = 42;
let y: u16 = 42;
// ...
let z: u256 = 42;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:integers}}
```

Unlike booleans, integer types need to be inferred. In most of the cases, the compiler will infer
Expand All @@ -84,9 +78,7 @@ infer the type and will require an explicit type annotation. It can either be pr
assignment or by using a type suffix.

```move
// Both are equivalent
let x: u8 = 42;
let x = 42u8;
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:integer_explicit_type}}
```

### Operations
Expand All @@ -102,6 +94,9 @@ multiplication, division, and remainder. The syntax for these operations is:
| % | modular division | The divisor is 0 |
| / | truncating division | The divisor is 0 |

> For more operations, including bitwise operations, please refer to the
> [Move Reference](/reference/primitive-types/integers.html#bitwise).
The type of the operands _must match_, otherwise, the compiler will raise an error. The result of
the operation will be of the same type as the operands. To perform operations on different types,
the operands need to be cast to the same type.
Expand All @@ -114,22 +109,19 @@ the operands need to be cast to the same type.
Move supports explicit casting between integer types. The syntax for it is:

```move
(<expression> as <type>)
<expression> as <type>
```

Note, that it requires parentheses around the expression to prevent ambiguity.
Note, that it may require parentheses around the expression to prevent ambiguity.

```move
let x: u8 = 42;
let y: u16 = (x as u16);
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:cast_as}}
```

A more complex example, preventing overflow:

```move
let x: u8 = 255;
let y: u8 = 255;
let z: u16 = (x as u16) + ((y as u16) * 2);
{{#include ../../../packages/samples/sources/move-basics/primitive-types.move:overflow}}
```

### Overflow
Expand Down
3 changes: 2 additions & 1 deletion packages/reference/sources/abilities.move
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#[allow(unused_field)]
module ref::abilities {

// ANCHOR: annotating_structs
Expand All @@ -10,7 +11,7 @@ public struct MyVec(vector<u64>) has copy, drop, store;
// ANCHOR_END: annotating_structs

// ANCHOR: conditional_abilities
public struct Cup<T> has copy, drop, store, key { item: T }
// public struct Cup<T> has copy, drop, store, key { item: T }
// ANCHOR_END: conditional_abilities

}
43 changes: 43 additions & 0 deletions packages/samples/sources/move-basics/primitive-types.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,47 @@

module book::primitive_types {

#[test, allow(unused_variable, unused_let_mut)]
fun test_primitive_types() {

// ANCHOR: variables_and_assignment
let x: bool = true;
let mut y: u8 = 42;
// ANCHOR_END: variables_and_assignment

// ANCHOR: shadowing
let x: u8 = 42;
let x: u16 = 42;
// ANCHOR_END: shadowing

// ANCHOR: boolean
let x = true;
let y = false;
// ANCHOR_END: boolean

// ANCHOR: integers
let x: u8 = 42;
let y: u16 = 42;
// ...
let z: u256 = 42;
// ANCHOR_END: integers

// ANCHOR: integer_explicit_type
// Both are equivalent
let x: u8 = 42;
let x = 42u8;
// ANCHOR_END: integer_explicit_type

// ANCHOR: cast_as
let x: u8 = 42;
let y: u16 = x as u16;
let z = 2 * (x as u16); // ambiguous, requires parentheses
// ANCHOR_END: cast_as

// ANCHOR: overflow
let x: u8 = 255;
let y: u8 = 255;
let z: u16 = (x as u16) + ((y as u16) * 2);
// ANCHOR_END: overflow
}
}
1 change: 0 additions & 1 deletion theme/head.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-B1E7R0BHX4');
</script>

0 comments on commit 90b7388

Please sign in to comment.