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.
Adds associated types documentation. (FuelLabs#5128)
## Description Adds to the Sway book a new entry for associated types explaining what they are and how they can be used. Adds associated types to reference docs. ## Checklist - [ ] 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). - [ ] I have added tests that prove my fix is effective or that my feature works. - [x] 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. --------- Co-authored-by: Joshua Batty <[email protected]>
- Loading branch information
1 parent
ff5ce9d
commit 8d8eda3
Showing
9 changed files
with
138 additions
and
1 deletion.
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,68 @@ | ||
# Associated Types | ||
|
||
Associated types in Sway allow you to define placeholder types within a trait, which can be customized by concrete | ||
implementations of that trait. These associated types are used to specify the return types of trait methods or to | ||
define type relationships within the trait. | ||
|
||
Associated types are a powerful feature of Sway's trait system, enabling generic programming and abstraction over | ||
types. They help improve code clarity and maintainability by allowing you to define generic traits without committing | ||
to specific types. | ||
|
||
## Declaring Associated Types | ||
|
||
Associated types are declared within a trait using the type keyword. Here's the syntax for declaring an associated type: | ||
|
||
```sway | ||
trait MyTrait { | ||
type AssociatedType; | ||
} | ||
``` | ||
|
||
## Implementing Associated Types | ||
|
||
Concrete implementations of a trait with associated types must provide a specific type for each associated type | ||
defined in the trait. Here's an example of implementing a trait with an associated type: | ||
|
||
```sway | ||
struct MyStruct; | ||
impl MyTrait for MyStruct { | ||
type AssociatedType = u32; // Implementing the associated type with u32 | ||
} | ||
``` | ||
|
||
In this example, `MyStruct` implements `MyTrait` and specifies that the associated type `AssociatedType` is `u32`. | ||
|
||
## Using Associated Types | ||
|
||
Associated types are used within trait methods or where the trait is used as a bound for generic functions or | ||
structs. You can use the associated type like any other type. Here's an example: | ||
|
||
```sway | ||
trait MyTrait { | ||
type AssociatedType; | ||
fn get_value(self) -> Self::AssociatedType; | ||
} | ||
struct MyStruct; | ||
impl MyTrait for MyStruct { | ||
type AssociatedType = u32; | ||
fn get_value(self) -> Self::AssociatedType { | ||
42 | ||
} | ||
} | ||
``` | ||
|
||
In this example, `get_value` is a trait method that returns an associated type `AssociatedType`. | ||
|
||
## Use Cases | ||
|
||
Associated types are particularly useful in scenarios where you want to define traits that work with different | ||
types of data structures or abstractions, allowing the implementor to specify the concrete types. Some common use cases include: | ||
|
||
- Collections: Traits for generic collections that allow users to specify the type of elements. | ||
- Iterator Patterns: Traits for implementing iterators with varying element types. | ||
- Serialization and Deserialization: Traits for serializing and deserializing data with different data formats. |
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
2 changes: 2 additions & 0 deletions
2
docs/reference/src/code/language/traits/associated-types/.gitignore
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,2 @@ | ||
out | ||
target |
3 changes: 3 additions & 0 deletions
3
docs/reference/src/code/language/traits/associated-types/Forc.lock
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,3 @@ | ||
[[package]] | ||
name = 'associated-consts' | ||
source = 'member' |
8 changes: 8 additions & 0 deletions
8
docs/reference/src/code/language/traits/associated-types/Forc.toml
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 = "lib.sw" | ||
license = "Apache-2.0" | ||
name = "associated-types" | ||
implicit-std = false | ||
|
||
[dependencies] |
26 changes: 26 additions & 0 deletions
26
docs/reference/src/code/language/traits/associated-types/src/lib.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
script; | ||
|
||
trait TypeTrait { | ||
type T; | ||
|
||
fn method(self, s1: Self::T) -> Self::T; | ||
} | ||
|
||
struct Struct {} | ||
|
||
struct Struct2 {} | ||
|
||
impl TypeTrait for Struct2 { | ||
type T = Struct; | ||
|
||
fn method(self, s1: Self::T) -> Self::T { | ||
s1 | ||
} | ||
} | ||
|
||
fn main() -> u32 { | ||
Struct2{}.method(Struct{}); | ||
|
||
1 | ||
} | ||
|
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