I have this code where I've written a generic scanner trait for a parser. The basic problem I have is summarized in the code below. My actual code is similar.
trait t{
fn o<T>(&self, g: T);
}
struct h;
impl t for h{
fn o<Vec<i32>>(&self, g: Vec<i32>){}
}
Then I get an error like this :
expected one of ,, :, =, or >
If however I type alias the complex type I don't get the error. But because of the way I need my code to behave I have to specialize it this way so there's no other alternative.
Is this a compiler issue or is there a syntactic way to do it?
I mean you can't specialize functions in this way, when you implement t, you must implement o as fn o<T>(&self, g: T), without specializing the type parameter. (in general you can't specialize anything right now, that's blocked in nightly under the specialization feature)
Thanks I understood the V shadowing part but I wanted to know why the compiler accepts o<Box>. Of course I could ignore implementing the trait for this particular scanner and have just have a method named o<Box<dyn io::Read>> but then I wouldn't be able to use this particular scanner as a trait object if ever I wanted to.
Edit:
I got it! You're saying that any name for the type parameter I give will shadow the outer name. Is that what you are saying? But should using i32 not give me an error? Or is it just a wrapper?
The compiler accepts o<Box> because it's a valid generic name. Like fn do_stuff<Trait>(t: Trait), here Trait is just the name of a generic. Even i32 is a valid name but the compiler will ask you to make it pascal case.
If you really want to introduce some genericity you can make the trait generic or add an associated type.