Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upInherent `impl`s for traits #1971
Comments
This comment has been minimized.
This comment has been minimized.
|
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!") }
} |
This comment has been minimized.
This comment has been minimized.
|
@mark-i-m No, this is not about methods of the trait, but methods for the trait object. |
This comment has been minimized.
This comment has been minimized.
|
@eddyb Thanks for the clarification! That makes a lot more sense now |
This comment has been minimized.
This comment has been minimized.
|
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
|
This comment has been minimized.
This comment has been minimized.
|
I suppose the syntax |
This comment has been minimized.
This comment has been minimized.
|
I feel like |
This comment has been minimized.
This comment has been minimized.
|
Frankly it's correct syntax if you want to add methods to all the types that implement a trait. |
This comment has been minimized.
This comment has been minimized.
|
I'd think A transparent wrapper scheme might look like an attribute like say |
djmcgill
referenced this issue
Jul 24, 2017
Closed
add some unsafe method to run code before RAM is initialized #17
burdges
referenced this issue
Sep 14, 2017
Merged
`dyn Trait` Syntax for Trait Objects: Take 2 #2113
Centril
added
the
T-lang
label
Dec 6, 2017
This comment has been minimized.
This comment has been minimized.
|
Closing in favor of #1880. |
burdges commentedApr 13, 2017
•
edited
As written, this code works but the commented out line gives error[E0576]: cannot find method or associated constant
meowin traitFoo.If we want an inherent-like method
meowfor 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
impls 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
structthat has the same name as the traitFoo:In this way, if
Foois defined in moduleAandmeowin moduleBthen the visibility ofmeowcan be controlled as you would control the visibility ofB::Foo.