Jump to content
  • Advertisement
Sign in to follow this  
FxMazter

[.net] generic parameter inferrance?

This topic is 4266 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sup everyone! I'm not exactly friend with the visual studio .net compiler ! :( I don't know why, but the compiler just doesn't want to figure out the type parameters for me argh! Ok, so here it goes:
interface TestInterface<V, Z> {}
class TestClass : TestInterface<String, int> {}

public void test<U,V,Z>(U t)
    where U: TestInterface<V,Z>
{
}


So, why can't the compiler figure out this: test(new TestClass()); I mean, common! I have given it everything it needs to know? The parameters cannot be inferred blahblah... I have to do it this way: test<TestClass, String, int>(new TestClass()); I know I can do like this:
public void test<V,Z>(TestInterface<V,Z> t)
{
}

But I really need to know the real input of "U" C ya laters!

Share this post


Link to post
Share on other sites
Advertisement
I think the problem is (at least in part) due to the fact that classes can implement multiple interfaces, and the same generic interface can be implemented multiple times with different parameters, for instance:


class TestClass : TestInterface<string, int>, TestInterface<string, float> {}



Inference is problematic here, even if you only have one instance of the generic interfae implemented, becuase if you add a new interface on to the class you would break any pieces of code which relied on the inference.

Share this post


Link to post
Share on other sites
The compiler doesn't consider the constraints when infering parameters, just the actual function arguments. I'm sure there's a logic behind the decision, but I couldn't say what it was. The important thing is that if you don't want to specify them manually, you have to explicitly reference each of the parameters in the argument list.

CM

Share this post


Link to post
Share on other sites
Dang, I have a custom made game engine in java with a build custom tailored library with datastructures and algorithms.

I was thinking to port it to c#, though now I'm not so sure anymore - I don't really feel like redesigning the whole datastructure framework :(

This is how it looks in java:



public static
<U extends IReadablePosition<V,T>,
V extends IForwardAccessor<V, ? extends IReadablePosition<V,T>>,
T>
void forward(U pos)
{
}




Yes, It looks very ugly... but it has some really nice functionality and the user of the function never has to think about the type parameters anyway :P

bye!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!