A simple question

Rob T rob at ucora.com
Thu Nov 15 19:32:03 PST 2012


On Friday, 16 November 2012 at 02:55:54 UTC, Stugol wrote:

> Also, I'm having difficulty specifying a default specialisation 
> for a template class:
>
>    class Event(TEventArgs : EventArgs = EventArgs) {
>    }
>
> Usage:
>
>    Event!() e1;     // Works
>    Event e2;        // Won't compile
>
> How can I have "Event" be an alias for "Event!EventArgs"?

I encountered this same "wtf???" today. Apparently, even with a 
default type specified, you still have to put in a !(). I don't 
know what the reasoning was behind making this seeming redundancy 
a requirement, and it partially defeats the purpose of the 
default. IN my case I wanted the default type to be selected when 
left unspecified, but also to get rid of the !() requirement for 
a more natural appearance. I was planning on asking about this in 
the forums later.

In any case, a simple work-a-round solution is to create an 
alias, for example:

alias Event!() Event_t;
// you may substitute "Event_t" with whatever type name you want

Event_t e2;        // Will compile!

BTW, I come from a C++ background, and I found that certain parts 
of D were more difficult to learn perhaps because of my C++ 
background. I think this may be because I expected things like 
modules to work like .h files, but they don't, or I expected copy 
and move semantics to work the same way in D but they don't, or I 
expected structs and classes to be similar to what C++ provides, 
but they are in fact very different in terms of how they operate. 
The GC is another matter to get used to. The worse of it is a 
lack of detailed documentation. The very best thing you can do, 
is get a copy of "The D Programming Language", there's also an 
on-line tutorial e-book, translated from Turkish into English 
over here http://ddili.org/ders/d.en/index.html but it is not yet 
fully translated.

Good luck, and have some patience, you'll need it.

--rt




More information about the Digitalmars-d mailing list