-
Notifications
You must be signed in to change notification settings - Fork 177
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
Add where clause on an impl #4508
Comments
Apparently Rust also allows the above to be written as |
The error you get if you don't add the
I found this weird - how could there be a matching impl if |
@nventuro there can still be a matching impl for a generic impl<T> Trait for T {
...
} |
# Description ## Problem\* Resolves #4508 ## Summary\* This enables the simple functionality of being able to declare a where clause directly on a struct implementation such as below: ```rust impl<T> MyStruct<T> where T: MyEq { fn my_eq(self, other: Self) -> bool { (self.a == other.a) & self.b.my_eq(other.b) } } ``` The code above is essentially syntactic sugar where we now every method in the struct impl a where clause. As the logic for resolving trait constraints already exists this PR really just updates the parser to accept `where` clauses and during definition collection attaches them to each method in an impl. ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [X] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [X] I have tested the changes locally. - [X] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: jfecher <jake@aztecprotocol.com>
Problem
I want to write an impl on some generic type
T
which I use in some functions that require thatT
implements some trait. This requires restricting each impl function withwhere T: Trait
, which leads to a lot of repetition.Happy Case
Ideally I'd simply do
impl<T> MyStruct<T> where T: Trait { ... }
instead of having to restrictT
in each function. This is of course less granular, but I imagine in most scenarios this coarse restriction would be fine.This happens in many places in aztec-nr, where most if not all functions need the clause.
Ultimately this is just syntax sugar though.
Project Impact
Nice-to-have
The text was updated successfully, but these errors were encountered: