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

chore(docs): Docs for turbofish operator #5555

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Changes from 2 commits
Commits
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
46 changes: 46 additions & 0 deletions docs/docs/noir/concepts/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,49 @@ impl Eq for MyStruct {
```

You can find more details on traits and trait implementations on the [traits page](../concepts/traits.md).

## Turbofish

Paths with generic parameters in expressions must prefix the opening brackets with a `::<>` operator.
The name "turbofish" comes from that `::<>` looks like a little fish.
jfecher marked this conversation as resolved.
Show resolved Hide resolved

Examples:
```rust
fn double<let N: u32>() -> u32 {
N * 2
}
fn example() {
assert(double::<9>() == 18);
assert(double::<7 + 8>() == 30);
}
```
```rust
trait MyTrait {
fn ten() -> Self;
}

impl MyTrait for Field {
fn ten() -> Self { 10 }
}

struct Foo<T> {
inner: T
}

impl<T> Foo<T> {
fn generic_method<U>(_self: Self) -> U where U: MyTrait {
U::ten()
}
}

fn example() {
let foo: Foo<Field> = Foo { inner: 1 };
// Using a type other than `Field` here (e.g. u32) would fail as
// there is no matching impl for `u32: MyTrait`.
//
// Substituting the `10` on the left hand side of this assert
// with `10 as u32` would also fail with a type mismatch as we
// are expecting a `Field` from the right hand side.
assert(10 as u32 == foo.generic_method::<Field>());
}
```
Loading