Recursive aliases?

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 23 04:44:10 PDT 2012


On Sat, 21 Apr 2012 15:46:29 -0400, Mehrdad <wfunction at hotmail.com> wrote:

> alias int delegate(out ItemGetter next) ItemGetter;
>
> We currently can't do the above^ in D, but what do people think about  
> allowing it?
> i.e. More specifically, I think an alias expression should be able to  
> refer to the identifier (unless it doesn't make sense, like including a  
> struct inside itself).
> (It would require a look-ahead.)

It doesn't work.

If I do this:

alias int delegate() dtype;

alias int delegate(dtype d) ddtype;

pragma(msg, ddtype.stringof);

I get:

int delegate(int delegate() d)

Note how it's not:

int delegate(dtype d)

Why?  Because alias does not create a new type.  It's a new symbol that  
*links* to the defined type.

How shall the compiler expand your example so it can know the type?  It  
works for classes because classes are a new type which do not need to have  
another type backing it.

The solution?  Well, there are two possible ones.  One is you create a  
ficticious type (or use void *) and cast when inside the delegate.  The  
other is to not use delegates, but use types instead.  Either a struct or  
a class/interface will do, something for the compiler to anchor on.

-Steve


More information about the Digitalmars-d mailing list