Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] generic parameter inferrance?

Started by FxMazter Sep 20, 2006 at 3:20 PM 2 replies 900+ views
Original Post
FxMazter
FxMazter
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(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!
vermilion_wizard
vermilion_wizard
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.
Conner McCloud
Conner McCloud
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
FxMazter
FxMazter
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    ,    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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.