Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize Sway docs and compress some sections #980

Merged
merged 31 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a1ec43f
Add instructions to show how to link the libraries
Mar 21, 2022
9efbd01
Reorganize docs
Mar 21, 2022
9177f6e
Add comments and logging
Mar 21, 2022
e73c9ba
Remove the missing else syntax
Mar 21, 2022
c05d216
Remove the println statements
Mar 22, 2022
11dc278
Cast address
Mar 22, 2022
4173b50
Update docs/src/sway-program-types/libraries.md
yirenlu92 Mar 22, 2022
44a7ad7
Update docs/src/basics/control_flow.md
yirenlu92 Mar 22, 2022
6324283
Update docs/src/basics/comments_and_logging.md
yirenlu92 Mar 22, 2022
8721dde
Update docs/src/basics/comments_and_logging.md
yirenlu92 Mar 22, 2022
2827c3e
Update docs/src/basics/comments_and_logging.md
yirenlu92 Mar 22, 2022
812ae59
Update docs/src/sway-program-types/smart_contracts.md
yirenlu92 Mar 22, 2022
106d7ea
Update examples with Address and ContractId
Mar 22, 2022
52f404b
Comment out storage syntax section
Mar 24, 2022
cfe457b
Fix subcurrency example so it builds
Mar 24, 2022
2682270
Constants cannot be addresses right now
Mar 24, 2022
1236659
Update docs/src/basics/control_flow.md
yirenlu92 Mar 24, 2022
00180b5
Update docs/src/basics/control_flow.md
yirenlu92 Mar 24, 2022
110a891
Update docs/src/basics/comments_and_logging.md
yirenlu92 Mar 24, 2022
17c0fd1
Update docs/src/basics/control_flow.md
yirenlu92 Mar 24, 2022
40de11b
Update docs/src/reference/rust_differences.md
yirenlu92 Mar 24, 2022
1394a07
Update docs/src/basics/control_flow.md
yirenlu92 Mar 24, 2022
f823419
Update docs/src/sway-program-types/index.md
yirenlu92 Mar 24, 2022
e14983e
Update control flow and make small edits
Mar 25, 2022
7c541dd
Fix merge conflict
Mar 25, 2022
71c80e8
Update examples/subcurrency/src/main.sw
yirenlu92 Mar 25, 2022
13dfa32
Update docs/src/basics/methods_and_associated_functions.md
yirenlu92 Mar 25, 2022
01edb81
Update docs/src/basics/control_flow.md
yirenlu92 Mar 25, 2022
0a7fe19
Update docs/src/basics/control_flow.md
yirenlu92 Mar 25, 2022
df751b6
Update docs/src/basics/control_flow.md
yirenlu92 Mar 25, 2022
0d46c0c
Update docs/src/sway-program-types/libraries.md
yirenlu92 Mar 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,40 @@
- [SDK](./sdks/index.md)
- [Rust SDK](./sdks/rust-sdk.md)
- [TypeScript SDK](./sdks/typescript-sdk.md)
- [Basics](./basics/index.md)
- [Overview](./basics/overview.md)
- [Program Types](./sway-program-types/index.md)
- [Contracts](./sway-program-types/smart_contracts.md)
- [Libraries](./sway-program-types/libraries.md)
- [Scripts](./sway-program-types/scripts.md)
- [Predicates](./sway-program-types/predicates.md)
- [Sway Language Basics](./basics/index.md)

- [Variables](./basics/variables.md)
- [Built-in Types](./basics/built_in_types.md)
- [Custom Types](./basics/custom_types.md)
- [Blockchain Types](./basics/blockchain_types.md)
- [Functions](./basics/functions.md)
<!-- - [Reference Types](./basics/reference_types.md) -->
- [Control Flow](./control-flow/index.md)
- [Loops](./control-flow/loops.md)
- [Structs and Tuples](./basics/structs_and_tuples.md)
- [Methods and Associated Functions](./basics/methods_and_associated_functions.md)
- [Comments and Logging](./basics/comments_and_logging.md)
- [Control Flow](./basics/control_flow.md)
- [Reference Types](./basics/reference_types.md)

- [Blockchain Development with Sway](./blockchain-development/index.md)
- [Contract Storage](./blockchain-development/storage.md)
- [Function Purity](./blockchain-development/purity.md)
- [Identifiers](./blockchain-development/identifiers.md)
- [Native Assets](./blockchain-development/native_assets.md)
- [Testing](./testing/index.md)

- [Testing with Rust](./testing/testing-with-rust.md)
- [Testing with TypeScript](./testing/testing-with-typescript.md)
- [Sway on the Chain](./sway-on-chain/index.md)
- [Program Types](./sway-on-chain/program_types.md)
- [Contracts](./sway-on-chain/smart_contracts.md)
- [Libraries](./sway-on-chain/libraries.md)
- [Scripts](./sway-on-chain/scripts.md)
- [Predicates](./sway-on-chain/predicates.md)
- [Smart Contract Development](./smart-contract-development/index.md)
- [Contract Storage](./smart-contract-development/storage.md)
- [Function Purity](./smart-contract-development/purity.md)

- [Advanced Concepts](./advanced/index.md)
- [Generic Types](./advanced/generic_types.md)
- [Traits](./advanced/traits.md)
- [Trait Constraints](./advanced/trait_constraints.md)
- [Assembly](./advanced/assembly.md)
- [Blockchain Concepts](./blockchain-concepts/index.md)
- [Blockchain Types](./blockchain-concepts/blockchain_types.md)
- [Identifiers](./blockchain-concepts/identifiers.md)
- [Native Assets](./blockchain-concepts/native_assets.md)
- [Conventions](./style/index.md)
- [Capitalization](./style/capitalization.md)
- [Reference](./reference/index.md)
- [Style Guide](./reference/style_guide.md)
- [Temporary Workarounds](./reference/temporary_workarounds.md)
- [Differences From Solidity](./reference/solidity_differences.md)
- [Differences From Rust](./reference/rust_differences.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Blockchain Types

Sway has a selection of types provided via the standard library ([`lib-std`](https://github.com/FuelLabs/sway-lib-std)) which both add a degree of type-safety, as well as make the intention of the developer more clear.
Sway is fundamentally a blockchain language, and it offers a selection of types tailored for the blockchain use case.

These are provided via the standard library ([`lib-std`](https://github.com/FuelLabs/sway-lib-std)) which both add a degree of type-safety, as well as make the intention of the developer more clear.

## `Address` Type

Expand Down
37 changes: 37 additions & 0 deletions docs/src/basics/comments_and_logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Comments and Logging

## Comments

Comments in Sway start with two slashes and continue until the end of the line. For comments that extend beyond a single line, you'll need to include `//` on each line.

```sway
// hello world
```

```sway
// let's make a couple of lines
// commented.
```

You can also place comments at the ends of lines containing code.

```sway
fn main() {
let baz = 8; // Eight is a lucky number
}
```

## Logging

To log integers, you can use the `log_u64`, `log_u32`, `log_u16`, or `log_u8` functions from the `lib-std` library.

```sway
use std::chain::log_u64;

fn main() {
let baz = 8;
log_u64(baz);
}
```

Note that you cannot log arbitrary structs yet because we do not yet support serialization.
85 changes: 85 additions & 0 deletions docs/src/basics/control_flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Control Flow

## `if` expressions

Sway supports _if_, _else_, and _else if_ expressions that allow you to branch your code depending on conditions.

For example:

```sway
fn main() {
let number = 6;

if number % 4 == 0 {
// do something
} else if number % 3 == 0 {
// do something else
} else {
// do something else
}; // <------------ note this semicolon
}
```

In Sway, note that a _statement_ is a _declaration **or** expression with a semicolon after it_. This means that you need to add a semicolon after an `if` to turn it into a statement, if it is being used for control flow.

This need for a semicolon after if expressions to turn them into statements will be removed eventually, but it hasn't been removed yet.

### Using `if` in a `let` statement

Like Rust, `if`s are expressions in Sway. What this means is you can use `if` expressions on the right side of a `let` statement to assign the outcome to a variable.

```sway
let my_data = if some_bool < 10 { foo() } else { bar() };
yirenlu92 marked this conversation as resolved.
Show resolved Hide resolved
```

Note that all branches of the `if` expression must return a value of the same type or the code will panic.

## Loops

### `while`

Loops in Sway are currently limited to `while` loops. This is what they look like:

```sway
while counter < 10 {
counter = counter + 1;
}
```

You need the `while` keyword, some condition (`value < 10` in this case) which will be evaluated each iteration, and a block of code inside the curly braces (`{...}`) to execute each iteration.

### `break` and `continue`

There are no `break` or `continue` keywords yet, but [they're coming](https://github.com/FuelLabs/sway/issues/587).

For now, the way to break out of a `while` loop early is to manually invalidate the condition. In this case, that just means setting `counter` to be >= 10.

Building on the previous example, here's what that might look like:

```sway
let mut counter = 0;
let mut break_early = false;
while counter < 10 {
yirenlu92 marked this conversation as resolved.
Show resolved Hide resolved
if break_early == true {
// here we ensure the condition will evaluate to false, breaking the loop
counter = 10
yirenlu92 marked this conversation as resolved.
Show resolved Hide resolved
} else {
// calling some other function to set the bool value
break_early = get_bool_value();
counter = counter + 1;
};
}
```

### Nested loops

You can also use nested `while` loops if needed:

```sway
while condition_1 == true {
// do stuff...
while condition_2 == true {
// do more stuff...
}
}
```
4 changes: 3 additions & 1 deletion docs/src/basics/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ fn equals(first_param: u64, second_param: u64) -> bool {
}
```

We have just declared a function named `equals` which takes two parameters: `first_param` and `second_param`. This function also returns a `bool` value, i.e. either `true` or `false`. This function returns `true` if the two given parameters are equal, and `false` if they are not. If we want to use this function, we can do so like this:
We have just declared a function named `equals` which takes two parameters: `first_param` and `second_param`. The parameters must both be 64-bit unsigned integers.

This function also returns a `bool` value, i.e. either `true` or `false`. This function returns `true` if the two given parameters are equal, and `false` if they are not. If we want to use this function, we can do so like this:

```sway
fn main() {
Expand Down
12 changes: 8 additions & 4 deletions docs/src/basics/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Basics
# Sway Language basics

Sway is a programming language designed for the FuelVM. It is a statically typed, compiled language with type inference and traits. Sway aims to make smart contract development safer and more performant through the use of strong static analysis and compiler feedback.

Sway basics.

- [Overview](./overview.md)
- [Variables](./variables.md)
- [Built-in Types](./built_in_types.md)
- [Custom Types](./custom_types.md)
- [Blockchain Types](./blockchain_types.md)
- [Functions](./functions.md)
<!-- - [Reference Types](./reference_types.md) -->
- [Structs and Tuples](./structs_and_tuples.md)
- [Methods and Associated Functions](./structs_and_tuples.md)
- [Control Flow](./control-flow.md)
- [Comments and Logging](./comments_and_logging.md)
- [Reference Types](./reference_types.md)
65 changes: 65 additions & 0 deletions docs/src/basics/methods_and_associated_functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Methods and Associated Functions

Methods are similar to functions in that we declare them with the `fn` keyword and they have parameters and return a value. However, unlike functions, _Methods_ are defined within the context of a struct (or enum), and either refers to that type or mutates it. The first parameter of a method is always `self`, which represents the instance of the struct the method is being called on.

_Associated functions_ are very similar to _methods_, in that they are also defined in the context of a struct or enum, but they do not actually use any of the data in the struct and as a result do not take _self_ as a parameter. Associated functions could be standalone functions, but they are included in a specific type for organizational or semantic reasons.

To declare methods and associated functions for a struct or enum, use an _impl block_. Here, `impl` stands for implementation.

```sway
script;

struct Foo {
bar: u64,
baz: bool,
}

impl Foo {
// this is a _method_, as it takes `self` as a parameter.
fn is_baz_true(self) -> bool {
self.baz
}

// this is an _associated function_, since it does not take `self` as a parameter.
fn new_foo(number: u64, boolean: bool) -> Foo {
Foo {
bar: number,
baz: boolean,
}
}
}

fn main() {
let foo = ~Foo::new_foo(42, true);
assert(foo.is_baz_true());
}
```

Note the syntax of the associated function call: `~Foo::new_foo(42, true);`. This bit of syntax is unique to Sway: when referring to a type directly, you preface the type with a tilde (`~`). To call an associated function, refer to the type and then the function name.
To call a method, simply use dot syntax: `foo.iz_baz_true()`.

## Syntax Examples

```sway
enum Color {
Blue : (),
Green : (),
Red : (),
Silver : (),
Grey : (),
// etc...
}

enum Make {
Ford : (),
Toyota : (),
Mazda : (),
Chevrolet : (),
BMW : (),
// etc...
}
struct Car {
make: CarMake,
color: Color,
}
```
5 changes: 0 additions & 5 deletions docs/src/basics/overview.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Custom Types
# Structs and Tuples

## Structs

Expand Down Expand Up @@ -120,68 +120,3 @@ let event = InventoryEvent::ItemLoss(Claim {
_This information is not vital if you are new to the language, or programming in general._

Enums do have some memory overhead. To know which variant is being represented, Sway stores a one-word (8-byte) tag for the enum variant. The space reserved after the tag is equivalent to the size of the _largest_ enum variant. So, to calculate the size of an enum in memory, add 8 bytes to the size of the largest variant. For example, in the case of `Color` above, where the variants are all `()`, the size would be 8 bytes since the size of the largest variant is 0 bytes.

## Methods and Associated Functions

_Methods_ are functions that are associated with a specific type and either refer to that type or mutate it. _Associated functions_ are very similar, but they do not use any of the data in the type. Associated functions could be standalone functions, but they
are included in a specific type for organizational or semantic reasons.

To declare methods and associated functions for a struct or enum, use an _impl block_. Here, `impl` stands for implementation.

```sway
script;

struct Foo {
bar: u64,
baz: bool,
}

impl Foo {
// this is a _method_, as it takes `self` as a parameter.
fn is_baz_true(self) -> bool {
self.baz
}

// this is an _associated function_, since it does not take `self` as a parameter.
fn new_foo(number: u64, boolean: bool) -> Foo {
Foo {
bar: number,
baz: boolean,
}
}
}

fn main() {
let foo = ~Foo::new_foo(42, true);
assert(foo.is_baz_true());
}
```

Note the syntax of the associated function call: `~Foo::new_foo(42, true);`. This bit of syntax is unique to Sway: when referring to a type directly, you preface the type with a tilde (`~`). To call an associated function, refer to the type and then the function name.
To call a method, simply use dot syntax: `foo.iz_baz_true()`.

## Syntax Examples

```sway
enum Color {
Blue : (),
Green : (),
Red : (),
Silver : (),
Grey : (),
// etc...
}

enum Make {
Ford : (),
Toyota : (),
Mazda : (),
Chevrolet : (),
BMW : (),
// etc...
}
struct Car {
make: CarMake,
color: Color,
}
```
7 changes: 0 additions & 7 deletions docs/src/blockchain-concepts/index.md

This file was deleted.

10 changes: 10 additions & 0 deletions docs/src/blockchain-development/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Blockchain Development with Sway

Sway is fundamentally a blockchain language. Because of this, it has some features and requirements that you may not have seen in general-purpose programming languages.

These are also some concepts related to the FuelVM and Fuel ecosystem that you may utilize when writing Sway.

- [Contract Storage](./storage.md)
- [Function Purity](./purity.md)
- [Identifiers](./identifiers.md)
- [Native Assets](./native_assets.md)
Loading