Thread overview
Suggestion: alternate function call syntax
Dec 26, 2002
Russell Lewis
Dec 27, 2002
Robert M. Münch
Dec 29, 2002
Ilya Minkov
Jan 06, 2003
Evan McClanahan
December 26, 2002
I really love C's function call syntax for its brevity and simplicity. But it can really be a pain sometimes when the API you are calling changes, or when the call has a huge number of useless (for you) arguments.  The problem I'm encountering is one like this.  Say I started with an API like this:

int foo(int operation,int x,int y);

So all my calls are coded that way.  But later, for some unknown reason, I decide the API reads a lot better like this:

int foo(int x,int operation,int y);

How do I make sure I've fixed all my calls?  Sure, I can do a global grep, but it would be slick if the language would give me some help.  So what I propose is to allow labeling of arguments in a call.  Such labelled arguments could even be given out of order:

   API:
   int foo(int operation,int x,int y);

   call:
   int op,x,y;
   ...
   int result = foo(operation:op,
                    x:x,
                    y:y);

This syntax would be a synonym of the old syntax.  Users could use either call syntax as they desired.  IMHO, this new syntax would be more readable to people not familiar with the API being called.  It would also allow the user to pass arguments in any order, based solely on what the user thought would be most readable.  So in the example above, we could keep the old API and change our calls to look like this:

   int result = foo(x:x, operation:op, y:y);

This would also make default arguments easy, since you could omit any argument anywhere in the list:

   API:
   int foo(int operation=ADD, int x,int y);

   call:
   int result = foo(x:x, y:y);	// ADD given automatically



Thoughts, anybody?

December 27, 2002
"Russell Lewis" <spamhole-2001-07-16@deming-os.org> schrieb im Newsbeitrag news:3E0B613C.5010004@deming-os.org...

> Thoughts, anybody?

Hi, IMO it's worth thinking about such a method you proposed. In C++ I often wished I could use default arguments not only at the end of a declaration. The disadvantage IMO is that the same API calls can look different to the reader... so you discover same things over and over. Further writing source-code analyzers could become harder... just my two EUR 0,02 Robert


December 29, 2002
Mixing through the order is not necessarily a good idea. Try to read some Visual Basic code, especially macros generated my Macro Recorder in word. Don't know how it's now, the version i've looked at generated a loooong tail of .Par="Option" which wasn't exactly easy to read, and could've been even worse if they were out-of-order. Another possible solution would be to use something like

FunCall( param1, --- , param3);

of course it would also work without the "---", but the readability and debugging would suffer. Maybe some other token would serve better. Generally i'm of the opinion, it's better to feed argument-greedy functions with constant structures. You could easily initialise such one in advance, or use some even more sophisticated initialisation, but if you need something simple, it should be possible to initialise an anonymous structure right inside the function call statement. I guess this has already worked in some C99 compilers. Back in Delphi i simply wrote a silly function for each such structure to assist me with generation. But i didn't have a liberty to leave anything out, which is a good one when assisted by ^such^ or similar syntax.

-i.

Robert M. Münch wrote:
> "Russell Lewis" <spamhole-2001-07-16@deming-os.org> schrieb im Newsbeitrag
> news:3E0B613C.5010004@deming-os.org...
> 
> 
>>Thoughts, anybody?
> 
> 
> Hi, IMO it's worth thinking about such a method you proposed. In C++ I often
> wished I could use default arguments not only at the end of a declaration.
> The disadvantage IMO is that the same API calls can look different to the
> reader... so you discover same things over and over. Further writing
> source-code analyzers could become harder... just my two EUR 0,02 Robert
> 
> 

January 06, 2003
i like this idea a lot.  The only problem being that there are no default arguments in D yet, so its most outstanding feature would be lost.  Still, it's something to consider, although I would imagine that it would involve a lot of changes in the language to get this syntax and the old one to work alongside each other.  I, for one, would vote for its inclusion, if Walter likes it.

Evan



Russell Lewis wrote:
> I really love C's function call syntax for its brevity and simplicity. But it can really be a pain sometimes when the API you are calling changes, or when the call has a huge number of useless (for you) arguments.  The problem I'm encountering is one like this.  Say I started with an API like this:
> 
> int foo(int operation,int x,int y);
> 
> So all my calls are coded that way.  But later, for some unknown reason, I decide the API reads a lot better like this:
> 
> int foo(int x,int operation,int y);
> 
> How do I make sure I've fixed all my calls?  Sure, I can do a global grep, but it would be slick if the language would give me some help.  So what I propose is to allow labeling of arguments in a call.  Such labelled arguments could even be given out of order:
> 
>    API:
>    int foo(int operation,int x,int y);
> 
>    call:
>    int op,x,y;
>    ...
>    int result = foo(operation:op,
>                     x:x,
>                     y:y);
> 
> This syntax would be a synonym of the old syntax.  Users could use either call syntax as they desired.  IMHO, this new syntax would be more readable to people not familiar with the API being called.  It would also allow the user to pass arguments in any order, based solely on what the user thought would be most readable.  So in the example above, we could keep the old API and change our calls to look like this:
> 
>    int result = foo(x:x, operation:op, y:y);
> 
> This would also make default arguments easy, since you could omit any argument anywhere in the list:
> 
>    API:
>    int foo(int operation=ADD, int x,int y);
> 
>    call:
>    int result = foo(x:x, y:y);    // ADD given automatically
> 
> 
> 
> Thoughts, anybody?
>