-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Inherent impl
s for traits
#1971
Comments
I didn't even know you could Also, would the same purpose be achieved if we had something like pub trait Foo {
#[no_override] // specifies that implementors cannot override the default
pub fn meow() { println!("Hello World!") }
} |
@mark-i-m No, this is not about methods of the trait, but methods for the trait object. |
@eddyb Thanks for the clarification! That makes a lot more sense now 😄 |
I see, thanks for the explanation @eddyb. In that case, the syntax I'm proposing should maybe be In some cases, one can write In that vein,
At least this way the inherent Also, I can improve the original trait based work around with
|
I suppose the syntax |
I feel like |
Frankly it's correct syntax if you want to add methods to all the types that implement a trait. |
I'd think A transparent wrapper scheme might look like an attribute like say |
Closing in favor of #1880. |
As written, this code works but the commented out line gives error[E0576]: cannot find method or associated constant
meow
in traitFoo
.If we want an inherent-like method
meow
for every type that satisfyingFoo
, but do not want clients modifying it, then we need a helper trait.Worse, we need new help traits for every inherent-like block we want to create, so our API gets polluted with several exposed helper traits. Also this
Foo::meow()
looks kinda iffy.I'd think inherent
impl
s could simply "work as expected" for traits, so all three call sites would call the original :For visibility purposes, these inherent trait methods could be treated as methods on an invisible wrapper
struct
that has the same name as the traitFoo
:In this way, if
Foo
is defined in moduleA
andmeow
in moduleB
then the visibility ofmeow
can be controlled as you would control the visibility ofB::Foo
.The text was updated successfully, but these errors were encountered: