What is this strange alias syntax?

Timon Gehr timon.gehr at gmx.ch
Mon May 23 06:32:47 PDT 2011


Steven Schveighoffer wrote:
> It is not perfectly consistent.  The function type syntax is useless,
> because you can only use it if you use the pointer modifier with it.  If
> you want to declare a function pointer, there are other (better) ways.

Yes, other ways of declaring a function pointer are better. But there is no other
way than alias for referring to a function type... It is not useless, see below.

>
> Yes, removing it would be a "special case" for the compiler, but it's
> syntax and usages are so bizzare, nobody would miss it.

I would. For the usages part, not the syntax part.

> it's akin to
> making:
>
> if(x);
>
> invalid. Yes, it's valid syntax, but it's almost certainly not what the
> user wanted.  It's special cased for failure, to aid the developer in
> writing less buggy programs.  This would be a similar change, and I
> actually thought it was already in the compiler.

It is in the compiler. It is actually not valid syntax anymore (disallowed by
grammar and caught by the parser). And it is not similar to removing function type
alias. You can write if(x){} as a replacement if you need it.

There is currently no alternative for the alias.

>
> I'll throw it back at you, do you see a good use case for it?  And no,
> porting C code isn't a good use case.
>
> -Steve

There are no alias in C code.

I actually gave one:
Timon Gehr wrote:
> alias int func();
>
> int main(){
>     static assert(is(typeof(main)==func);
>     return 0;
> }

You can use something similar to this to constrain templates. I do not say this
syntax is good. But at the moment we have nothing better than that. How DMD
handles function types is closer to how C does it than me or you would like, see
my other post.

BTW:

writeln(typeid(int function(int))); //int()*

wtf?


Timon


More information about the Digitalmars-d-learn mailing list