Generics - A question on generics - delegates - runtime binding.

G

Guest

I would like to do the following and cannot figure it out.

public class A<T>
{
public delegate T GetValueDelegate(A<T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}

public class B
{
public static int GetValue(A<int> var)
{
//do something and return int
}

public static float GetValue(A<float> var)
{
//do something and return float
}

public static string GetValue(A<string> var)
{
//do something and return string
}

//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
}

Now:-----------------------------------
I was thinking when I do

....somewhere in some code....
A<int> s = new A<int>();

that on creation of this instance of A of type int that the delegate
in A would find the proper overload in class B such that:
....
int I = s.GetValue();
....
would resolve to the class B's method
....
public static int GetValue(A<int> var)
....
and this would occur dynamically at runtime.


The short is that I get a 'No Overload Error' as follows.
'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'

I am obviously not understanding something. Help would be greatly appriciated.

k
 
S

Siva M

You have not defined a overload for GetValue in class B that takes no
parameters. But, you are calling it in class A<T>.

I would like to do the following and cannot figure it out.

public class A<T>
{
public delegate T GetValueDelegate(A<T> var);
public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);
}

public class B
{
public static int GetValue(A<int> var)
{
//do something and return int
}

public static float GetValue(A<float> var)
{
//do something and return float
}

public static string GetValue(A<string> var)
{
//do something and return string
}

//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}
}

Now:-----------------------------------
I was thinking when I do

....somewhere in some code....
A<int> s = new A<int>();

that on creation of this instance of A of type int that the delegate
in A would find the proper overload in class B such that:
....
int I = s.GetValue();
....
would resolve to the class B's method
....
public static int GetValue(A<int> var)
....
and this would occur dynamically at runtime.


The short is that I get a 'No Overload Error' as follows.
'No overload for 'GetValue' matches delegate 'A<T>.GetValueDelegate'

I am obviously not understanding something. Help would be greatly
appriciated.

k
 
G

Guest

Thank you for the reply but I do not think that is the reason. The delegate
in class A is defined with the signature. It is this signature that should
match against the methods in class B. And the methods in B do match.

public delegate T GetValueDelegate(A<T> var);

thanks, but I am still missing something.
 
B

Bernhard Huemer

Hello,
//or in general
public static SomeType GetValue(A<SomeType> var)
{
//do something specific for type SomeType and return that SomeType
}

I think you've supposed GetValue to be a generic method but it
isn't. Therefore no signature matches the on of your delegate.

public class B
{
// ...

public static SomeType GetValue<SomeType>(A<SomeType> var)
{
// ...
}
}

Btw. the overloaded versions won't get called, whether the signature
matches or not.

greets
 
G

Guest

Hey bigguy!

The problem is that A<T> can be instantiated with any Type T. Your B
overloads GetValue for some distinct A<T>'s: A<int>, A<float>, A<string>,
A<SomeType> but there will always be some type T (for example FooBar) that
it does not overload. Generic classes must be able to work for any T you
want to pameterize them with.

This seems to make them nearly useless since the only operations we can
perform on any type T are the ones in the common base class "object"

Enter constraints.

Constraints let us add operations that T can perform by constraining the
type of T

E.g
public class Foo<T> where T : IBar

So now we can use any operations that the IBar interface supports and in
turn we promise that whenever we instantiate a Foo we will pass a T that
implements IBar.

Now the bad news.
Interfaces don't support static members as part of their signature. So we
have no way to constrain the type to say that we will only try to create A<T>
with types that implement T GetValue(A<T>).

So what are you really trying to do?
 
D

dubian

Well,

Actually, I want to do this.

A<any_type> var1 = new A<any_type>();

//set up a etc...

any_type var2 = var1.GetValue();

where GetValue is dependant on var2.



Basically, I want a polymorfic collection class that does not need
boxing due to the number of objects I need to create and least of all,
I do not want to write 50 classes, I just want to write 50 GetValue()
methods that return a native type, not an object and thus have to cast
consistantly.

I find it odd that generics are so closely tied to constraints. Sort of
an oxymoronic relationship.

I understand that things need to bind with out exception, however, I
would rather chase a couple of exceptions than manage 50
implimentations of a generic idea. I get tired of subclassing.

And besides, wouldn't it be a compile time error if the type <T> didn't
have a corresponding overload available.

Thanks for your assistance BTH. It is helping me to sort out the
details here.
 
D

dubian

QUICK TYPE CLARIFICATION:

ORIGIONAL
where GetValue is dependant on var2

NEW
where GetValue is in sync with the type of var2
 
D

dubian

What I am really trying to do is put all the GetValue implimentations
in a single assembly that I can dynamically swap out at runtime. A
strategy pattern.
 
D

dubian

Additionally, Is it possible to configure the constraint's list to

where T : int, float, double, etc....

It seems to be giving me errors.
 
J

Jon Skeet [C# MVP]

dubian said:
Additionally, Is it possible to configure the constraint's list to

where T : int, float, double, etc....

It seems to be giving me errors.

Well, you can constrain T to be a value type:

where T : struct

The constraint you gave can't be applied because constraints are
"ANDed" - *every* constraint you specify has to be satisfied by the
candidate type, and clearly there are no types which are both ints and
floats :)
 
D

dubian

So I guess I have to subclass. It would be nice to have generic
delegates be able to resolve to a specialized matching overload. To
handle all other cases, a rule to require a generic function exist or
simply throw an exception would handle cases where no matching overload
was found.

This would be a nice feature and enable the following.

Sample<int> s = new Sample<int>(4,5,6,3,4,2,3,4);
Sample<char> c = new Sample<char>('a','b','c','d');

int Sum = s.GetValue();
int UniquesCount = c.GetValue();

where the method GetValue() can be routed with delegates to the a
specialized function using signature matching.

In general, when 95% of my operations on, say my Sample class, are
generic, but 5% is specialized, it would be nice to leverage that
commonality. I do not see where this implementation of generics by
Microsoft has made a big improvement of developer productivity.

Many may question my logic, but for those that have used PERL and
understand the power of indirection may see my logic. This may
introduce hard to understand code if you are coming behind someone, but
it also allows a powerful simplicity to be leveraged; and thus instead
of 2 weeks to subclass 50 items, it would take 1 day to specialize a
specific attribute of the code.

This problem could also be solved by allowing constraints of the form
where T: (int, float, char, string, classA, structB, interfaceC, etc)

I would like to know if anyone else agrees -

For example: I once wrote a packet parser where the header of the
packet aligned with the functions to handle those packet types. The
code looks like this.

$a = first4CharsOfCompletePacket(incommingPacket);
$$a(a);

If the first 4 chars (say 'ABBA') of the packet identify the
packet, then $$a(a) calls a function by the name ABBA that is
specialized to handle this specific packet. The call actually made by
$$a(a) is determined dynamically and proper error handling catches
malformed packets etc..


Otherwise, you have a big case statement to link packet type to packet
handler. When you have many packet types, that is a lot of work and
these two lines save thousands of lines in a large packet parser.

If PERL can do this on the CLR via visual PERL by ActiveState - then
so can C#.

Many programming problems naturally fit this scenario. It would be
refreshing to see a powerful language like C#, take a fresh direction
and leverage indirection formally and provide it as a tool that can be
used when the forces push us in this direction instead of adhering to
strict formalism where everything must be deterministic.

Call it bad programming or what ever, I call it meeting deadlines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Generics; OO 4
Help with generics 2
Bug or by design 3
here is a relief shader for WPF 1
Operator overload for generics 6
Anonymous Types, LINQ and Generics 3
generics problem 2
Passing Properties to Generics 2

Top