We need to kill C syntax for declaring function types

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 4 05:44:10 PDT 2010


On Mon, 04 Oct 2010 05:07:07 -0400, Don <nospam at nospam.com> wrote:

> A great example of how C syntax is hurting us.
> ---
> I found this bit of code in std.container, inside BinaryHeap:
>
>      size_t insert(ElementType!Store value)
>      {
>          static if (is(_store.insertBack(value)))
>          {
>              ...
>          }
>          else ...
>
> What does the static if do? It's *intended* to check if _store has a  
> member function insertBack(), which accepts type of a 'value'.
>
> But instead, it ALWAYS silently does the 'else' clause.
> Unless _store.insertBack is a valid *type*, (eg, alias int insertBack;).
> In which case it gives an error "declaration value is already defined".

I'm really confused here.  I could have sworn I've seen compile time  
checks like this everywhere.  Is the "bug" that's unflagged by the  
compiler that you are missing a typeof(...)?

>
> Why?
>
> This happens because
> x(y); is valid C syntax for declaring a type 'y', such that &y is of  
> type  'x function()'.

Wait, I thought when declaring a function pointer, you had to have the (*)  
in there?  Plus I thought you had to have an extra set of parentheses?   
I've never seen this before.

Trying...

Oh, that's freaking awful.  x(y); silently compiles into something  
useless, you need to declare it with a typedef in order to use it (and  
even then, it's horrible).  It's like accepting the line

int;

Yes, 100% agree, get rid of this.  Does that mean we need to get rid of  
C-style function pointer declarations?  Because I recently saw a use for  
them (deciphering C-style function pointer syntax).  Even if that has to  
go, I'm fine with it.

-Steve


More information about the Digitalmars-d mailing list