forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Result to blockchain types (FuelLabs#3894)
Added the Result type to the same page where Identity, ContractId, and Address are covered. Definition was taken from standard library repo - https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/result.sw --------- Co-authored-by: Camila Ramos <[email protected]> Co-authored-by: Mohammad Fawaz <[email protected]>
- Loading branch information
1 parent
91feedc
commit a1cacd6
Showing
11 changed files
with
125 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Commonly Used Library Types | ||
|
||
The Sway Standard Library is the foundation of portable Sway software, a set of minimal shared abstractions for the broader Sway ecosystem. It offers core types, library-defined operations on language primitives, native asset management, blockchain contextual operations, access control, storage management, and support for types from other VMs, among many other things. Reference the standard library docs [here](https://fuellabs.github.io/sway/master/std/index.html). | ||
|
||
## `Result<T, E>` | ||
|
||
Type `Result` is the type used for returning and propagating errors. It is an `enum` with two variants: `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error and containing an error value. The `T` and `E` in this definition are type parameters, allowing `Result` to be generic and to be used with any types. | ||
|
||
```sway | ||
{{#include ../../../../sway-lib-std/src/result.sw:docs_result}} | ||
``` | ||
|
||
Functions return `Result` whenever errors are expected and recoverable. Take the following example: | ||
|
||
```sway | ||
{{#include ../../../../examples/result/src/main.sw}} | ||
``` | ||
|
||
## `Option<T>` | ||
|
||
Type `Option` represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not. `Option` types are very common in Sway code, as they have a number of uses: | ||
|
||
- Initial values where `None` can be used as an initializer. | ||
- Return value for otherwise reporting simple errors, where `None` is returned on error. | ||
|
||
The implementation of `Option` matches on the variant: if it's `Ok` it returns the inner value, if it's `None`, it [reverts](https://github.com/FuelLabs/fuel-specs/blob/master/src/vm/instruction_set.md#rvrt-revert). | ||
|
||
```sway | ||
{{#include ../../../../sway-lib-std/src/option.sw:docs_option}} | ||
``` | ||
|
||
`Option` is commonly paired with pattern matching to query the presence of a value and take action, allowing developers to choose how to handle the `None` case. | ||
|
||
Below is an example that uses pattern matching to handle invalid divisions by 0 by returning an `Option`: | ||
|
||
```sway | ||
{{#include ../../../../examples/option/src/main.sw}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "option" | ||
|
||
[dependencies] | ||
std = { path = "../../sway-lib-std" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
script; | ||
|
||
fn divide(numerator: u64, denominator: u64) -> Option<u64> { | ||
if denominator == 0 { | ||
Option::None | ||
} else { | ||
Option::Some(numerator / denominator) | ||
} | ||
} | ||
|
||
fn main() { | ||
let result = divide(6, 2); | ||
// Pattern match to retrieve the value | ||
match result { | ||
// The division was valid | ||
Option::Some(x) => std::logging::log(x), | ||
// The division was invalid | ||
Option::None => std::logging::log("Cannot divide by 0"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "result" | ||
|
||
[dependencies] | ||
std = { path = "../../sway-lib-std" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
script; | ||
|
||
enum MyContractError { | ||
DivisionByZero: (), | ||
} | ||
|
||
fn divide(numerator: u64, denominator: u64) -> Result<u64, MyContractError> { | ||
if (denominator == 0) { | ||
return Result::Err(MyContractError::DivisionByZero); | ||
} else { | ||
Result::Ok(numerator / denominator) | ||
} | ||
} | ||
|
||
fn main() -> Result<u64, str[4]> { | ||
let result = divide(20, 2); | ||
match result { | ||
Result::Ok(value) => Result::Ok(value), | ||
Result::Err(MyContractError::DivisionByZero) => Result::Err("Fail"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters