Skip to content

Commit

Permalink
New example for methods and associated functions (FuelLabs#4226)
Browse files Browse the repository at this point in the history
## Description
Closes FuelLabs#4223

Basically just showing that associated methods do actually work.

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
mohammadfawaz authored Mar 6, 2023
1 parent 436ffe5 commit cfb5d48
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
27 changes: 1 addition & 26 deletions docs/book/src/basics/methods_and_associated_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,7 @@ _Associated functions_ are very similar to _methods_, in that they are also defi
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());
}
{{#include ../../../../examples/methods_and_associated_functions/src/main.sw}}
```

To call a method, simply use dot syntax: `foo.iz_baz_true()`.
Expand Down
5 changes: 5 additions & 0 deletions examples/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ name = 'match_statements'
source = 'member'
dependencies = ['std']

[[package]]
name = 'methods_and_associated_functions'
source = 'member'
dependencies = ['std']

[[package]]
name = 'msg_sender'
source = 'member'
Expand Down
1 change: 1 addition & 0 deletions examples/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
members = [
"abi_supertraits",
"arrays",
"methods_and_associated_functions",
"asm_return_tuple_pointer",
"break_and_continue",
"cei_analysis",
Expand Down
8 changes: 8 additions & 0 deletions examples/methods_and_associated_functions/Forc.toml
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 = "methods_and_associated_functions"

[dependencies]
std = { path = "../../sway-lib-std" }
26 changes: 26 additions & 0 deletions examples/methods_and_associated_functions/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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());
}

0 comments on commit cfb5d48

Please sign in to comment.