Generics and typedefs?

Posts   
 
    
louthy
User
Posts: 61
Joined: 02-Aug-2005
# Posted on: 13-Aug-2005 15:08:24   

I've not had a chance to look fully at the spec of C# 2.0. However I know it supports generics (templates from the C++ days). Does it by any chance support typedef or similar so ridiculously long variable declarations can be reduced?

ie.


MyGenericCollection<MyOtherGenericCollection<int>,YetOtherGenericCollection<int>> blah = new MyGenericCollection<MyOtherGenericCollection<int>,YetOtherGenericCollection<int>>();

In the good old days (wink ) you could do this:


typedef MyGenericCollection<MyOtherGenericCollection<int>,YetOtherGenericCollection<int>> Wibble;

Then define the type thus:


Wibble blah = new Wibble();

Yeah I know it kind of removes the point of generics, but when you know you're only going to be using say 5 or 6 variants of a generic class it's easier to typedef them.

I suppose I could do this:


public class Wibble : MyGenericCollection<MyOtherGenericCollection<int>,YetOtherGenericCollection<int>>
{
}

But then you need to implement the constructors etc.

Thoughts?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39801
Joined: 17-Aug-2003
# Posted on: 15-Aug-2005 10:20:42   

It doesn't support typedefs, but you can alias names, with using simple_smile See the syntax description of 'using' for more details on this simple_smile (so it's not only for namespaces and declaring a block)

Frans Bouma | Lead developer LLBLGen Pro
louthy
User
Posts: 61
Joined: 02-Aug-2005
# Posted on: 15-Aug-2005 22:00:45   

Otis wrote:

It doesn't support typedefs, but you can alias names, with using simple_smile See the syntax description of 'using' for more details on this simple_smile (so it's not only for namespaces and declaring a block)

Yep, I'm aware of that (although I've only ever used it to give a name to an ambiguous namespace), but you'd still have to do that in each file that used it. Then if you change the template parameter list, you'd then have to go and change all the code files that use it.

Or am I missing something? (wouldn't surprise me tbh, it was a heavy weekend wink )

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39801
Joined: 17-Aug-2003
# Posted on: 16-Aug-2005 11:50:06   

louthy wrote:

Otis wrote:

It doesn't support typedefs, but you can alias names, with using simple_smile See the syntax description of 'using' for more details on this simple_smile (so it's not only for namespaces and declaring a block)

Yep, I'm aware of that (although I've only ever used it to give a name to an ambiguous namespace), but you'd still have to do that in each file that used it. Then if you change the template parameter list, you'd then have to go and change all the code files that use it.

Or am I missing something? (wouldn't surprise me tbh, it was a heavy weekend wink )

No, you're correct. Though do you really want that? I always hated complex typedefs as these are hard to read. A classic:


typedef unsigned (__stdcall *PTHREAD_START) (void *);

I always get confused what gets defined as what, as this makes it possible to define:


m_ghInitSystemThread=(void *)_beginthreadex(NULL, 0, (PTHREAD_START)&InitSystem, NULL, 0, (unsigned int *)&m_glInitSystemThreadID);

but I mean... if you need them, isn't the language really up to a revision? simple_smile I already found it great that a pure typedef construct as available in C and C++, weren't available in C#, as it would make code harder to understand (IMHO).

Frans Bouma | Lead developer LLBLGen Pro
louthy
User
Posts: 61
Joined: 02-Aug-2005
# Posted on: 16-Aug-2005 19:06:18   

Otis wrote:

I already found it great that a pure typedef construct as available in C and C++, weren't available in C#, as it would make code harder to understand (IMHO).

I totally agree. The language is more 'pure' then. But I also remember some pretty hairy looking functions when using templates in C++, and they became significantly more readable with typedefs.

I'm all for language purity, but I'm also a big fan of code that's readable simple_smile