This post originated from an RSS feed registered with .NET Buzz
by Brad Wilson.
Original Post: Latent Typing and Generics
Feed Title: The .NET Guy
Feed URL: /error.aspx?aspxerrorpath=/dotnetguy/Rss.aspx
Feed Description: A personal blog about technology in general, .NET in specific, and when all else fails, the real world.
class TestClass<T>
{
public void Foo(T t)
{
t.Bar();
}
}
This is exactly what I mean about "latent typing". The C++ compiler doesn't care what type T is, as long as you can call Bar() on it. Using the generics system in .NET 2.0, you have to write something like:
interface IBar
{
void Bar();
}
class TestClass<T> where T : IBar
{
public void Foo(T t)
{
t.Bar();
}
}
What Charles didn't point out is that there's absolutely no point for generics here now. You can write this, today, in .NET 1.x, and get the same thing:
class TestClass
{
public void Foo(IBar t)
{
t.Bar();
}
}
And now it's less confusing. This leads me to the conclusion that generics, as implemented in .NET, are only good for a single class of problem: that is, problems where you don't actually USE the type in question. What kinds of problems are that? Well, basically, containers.
The generic system in .NET was designed solely to support performant, cast-free containers (a solution we could already solve well with code generation). The power and beauty of latent typed generic programming is completely absent from the generics solution in .NET.
The opinions expressed herein are solely those of Brad Wilson, and not meant as an endorsement of or by any other individuals or groups. This syndication is provided for the private, personal use of individuals. Unauthorized commercial reproduction is strictly prohibited.