www.digitalmars.com         C & C++   DMDScript  

D - Suggestion: alternate function call syntax

reply Russell Lewis <spamhole-2001-07-16 deming-os.org> writes:
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?
Dec 26 2002
next sibling parent reply "Robert M. Münch" <robert.muench robertmuench.de> writes:
"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
Dec 27 2002
parent Ilya Minkov <midiclub 8ung.at> writes:
Mixing through the order is not necessarily a good idea. Try to read=20
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=20
loooong tail of .Par=3D"Option" which wasn't exactly easy to read, and=20
could've been even worse if they were out-of-order. Another possible=20
solution would be to use something like

FunCall( param1, --- , param3);

of course it would also work without the "---", but the readability and=20
debugging would suffer. Maybe some other token would serve better.=20
Generally i'm of the opinion, it's better to feed argument-greedy=20
functions with constant structures. You could easily initialise such one =

in advance, or use some even more sophisticated initialisation, but if=20
you need something simple, it should be possible to initialise an=20
anonymous structure right inside the function call statement. I guess=20
this has already worked in some C99 compilers. Back in Delphi i simply=20
wrote a silly function for each such structure to assist me with=20
generation. But i didn't have a liberty to leave anything out, which is=20
a good one when assisted by ^such^ or similar syntax.

-i.

Robert M. M=FCnch wrote:
 "Russell Lewis" <spamhole-2001-07-16 deming-os.org> schrieb im Newsbeit=

 news:3E0B613C.5010004 deming-os.org...
=20
=20
Thoughts, anybody?

=20 Hi, IMO it's worth thinking about such a method you proposed. In C++ I =

 wished I could use default arguments not only at the end of a declarati=

 The disadvantage IMO is that the same API calls can look different to t=

 reader... so you discover same things over and over. Further writing
 source-code analyzers could become harder... just my two EUR 0,02 Rober=

=20
=20

Dec 28 2002
prev sibling parent Evan McClanahan <evan dontSPAMaltarinteractive.com> writes:
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?
 

Jan 06 2003