Proposal: Function declaration with keyword

boyd gaboonviper at gmx.net
Sat Apr 12 04:18:47 PDT 2008


Okay, so I've been working with D for a while now. I stumbled upon it when  
I was searching for a better language for me to use.

Other languages always seemed to lack some crucial feature. D is generally  
easy to use, easy to read and has all the options I wanted. I also love  
the attention it has given to little details that just make things so much  
easier.

It's not perfect by any means, but overal it's better than anything. I  
currently have gripes with mixins, which are a pain to use. From what I  
hear Macros are gong to replace them, so for now I'll avoid that subject.


Instead I've been wondering about a problem that, to my knowledge, hasn't  
gotten any attention yet: function declaration keyword.

One thing I never really liked about C++ is the way function declarations  
look. You can't easily differentiate them from variable declarations.  
Maybe it's partly because I started with Object Pascal rather than C++,  
but I never got used to it.

Anyway, why not use a keyword for function declarations?

   function void DoSomething(int someParam);

Here it's easier to figure out what it is than:

   void DoSomething(int someParam);

It also solves some storage class problems like:

   const int DoSomething(int someParam);

Is the function const? or is the return value const? With the current  
const system we know this means the return value is const, but it isn't  
obvious to anyone who doesn't know this.

   function const int DoSomething(int someParam);

This is much more natural and it has the advantage that storage classes  
meant for the function aren't confusing.

   static int DoSomething(int someParam);

does this return a static integer variable? We know it's not possible, but  
at first glance that might be what you see. Using the function keyword  
however, there is no such problem:

   static function int DoSomething(int someParam);

It also removes the need to have storage classes like pure and const at  
the end of the function.

   int DoSomething(int someParam) pure;

vs

   pure function int DoSomething(int someParam);

Starting function declarations with a keyword may seem like a small  
detail, but I think it can improve the expressiveness of D a lot.

Anyway, what do you guys think?

Cheers,
Boyd



More information about the Digitalmars-d mailing list