www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - function argument 'shorthand'

reply Dave <Dave_member pathlink.com> writes:
Couldn't find this in the archive, so I don't know if it's been discussed
before.

One of the things I like about Pascal is that you can specify function arguments
of the same type w/o repeating the type. So, in D we could:

int foo(int x, y, z) // y and z are type int
{}

int bar(int x = 1, y = 2, z = 3) // y and z are type int
{}

void baz(int x, y, double d, f = 3.14159)  // y is an int, f is a double
{}

Thoughts?
May 19 2006
next sibling parent reply "Derek Parnell" <derek psych.ward> writes:
On Sat, 20 May 2006 15:00:29 +1000, Dave <Dave_member pathlink.com> wrote:

 Couldn't find this in the archive, so I don't know if it's been discussed
 before.

 One of the things I like about Pascal is that you can specify function  
 arguments
 of the same type w/o repeating the type. So, in D we could:

 int foo(int x, y, z) // y and z are type int
 {}

 int bar(int x = 1, y = 2, z = 3) // y and z are type int
 {}

 void baz(int x, y, double d, f = 3.14159)  // y is an int, f is a double
 {}

 Thoughts?
It's not a good idea because it makes things too easy for coders to write mistakes. Maybe a compromise that makes it explicit that the coder is taking shortcuts?... int foo (int {x,y,z} ) void baz(int {x,y}, double {d. f=3.14159}) -- Derek Parnell Melbourne, Australia
May 21 2006
parent reply James Dunne <james.jdunne gmail.com> writes:
Derek Parnell wrote:
 On Sat, 20 May 2006 15:00:29 +1000, Dave <Dave_member pathlink.com> wrote:
 
 Couldn't find this in the archive, so I don't know if it's been discussed
 before.

 One of the things I like about Pascal is that you can specify 
 function  arguments
 of the same type w/o repeating the type. So, in D we could:

 int foo(int x, y, z) // y and z are type int
 {}

 int bar(int x = 1, y = 2, z = 3) // y and z are type int
 {}

 void baz(int x, y, double d, f = 3.14159)  // y is an int, f is a double
 {}

 Thoughts?
It's not a good idea because it makes things too easy for coders to write mistakes. Maybe a compromise that makes it explicit that the coder is taking shortcuts?... int foo (int {x,y,z} ) void baz(int {x,y}, double {d. f=3.14159})
Pascal syntax: function foo(x,y,z : int) : int procedure baz(x,y : int, d, f : double = 3.14159) I've always liked it, since it flows naturally while typing a declaration. You don't naturally think of the type before the parameter name (unless you're conditioned to do so). That, and it makes templating code much easier to read/write since you've got that leading function/procedure keyword to introduce more syntax with: function(T) foo(x,y,z : T) : T { return x + y + z; } -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O M-- V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e h>--->++ r+++ y+++ ------END GEEK CODE BLOCK------ James Dunne
May 22 2006
parent reply Dave <Dave_member pathlink.com> writes:
James Dunne wrote:
 Derek Parnell wrote:
 On Sat, 20 May 2006 15:00:29 +1000, Dave <Dave_member pathlink.com> 
 wrote:

 Couldn't find this in the archive, so I don't know if it's been 
 discussed
 before.

 One of the things I like about Pascal is that you can specify 
 function  arguments
 of the same type w/o repeating the type. So, in D we could:

 int foo(int x, y, z) // y and z are type int
 {}

 int bar(int x = 1, y = 2, z = 3) // y and z are type int
 {}

 void baz(int x, y, double d, f = 3.14159)  // y is an int, f is a double
 {}

 Thoughts?
It's not a good idea because it makes things too easy for coders to write mistakes. Maybe a compromise that makes it explicit that the coder is taking shortcuts?... int foo (int {x,y,z} ) void baz(int {x,y}, double {d. f=3.14159})
Pascal syntax: function foo(x,y,z : int) : int procedure baz(x,y : int, d, f : double = 3.14159)
I didn't want to take it that far :). Just far enough that "following params." are implicitly typed. The reason that feels natural to me is because I've gotten used to declaring local vars. that way, so I end up doing that for function params. and end-up having to back-track (more often than I'd like) <g>
 I've always liked it, since it flows naturally while typing a 
 declaration.  You don't naturally think of the type before the parameter 
 name (unless you're conditioned to do so).  That, and it makes 
 templating code much easier to read/write since you've got that leading 
 function/procedure keyword to introduce more syntax with:
 
 function(T) foo(x,y,z : T) : T {
     return x + y + z;
 }
 
May 22 2006
parent Johan Granberg <lijat.meREM OVEgmail.com> writes:
Dave wrote:
 I didn't want to take it that far :). Just far enough that "following 
 params." are implicitly typed. The reason that feels natural to me is 
 because I've gotten used to declaring local vars. that way, so I end up 
 doing that for function params. and end-up having to back-track (more 
 often than I'd like) <g>
I agree this is a feature that would save a lot of typing and would bee in line with the rest of the syntax.
May 22 2006
prev sibling parent reply Walter Bright <newshound digitalmars.com> writes:
Dave wrote:
 Couldn't find this in the archive, so I don't know if it's been discussed
 before.
 
 One of the things I like about Pascal is that you can specify function
arguments
 of the same type w/o repeating the type. So, in D we could:
 
 int foo(int x, y, z) // y and z are type int
 {}
 
 int bar(int x = 1, y = 2, z = 3) // y and z are type int
 {}
 
 void baz(int x, y, double d, f = 3.14159)  // y is an int, f is a double
 {}
 
 Thoughts?
It has grammatical ambiguities. Consider: int foo(int x, y); Is the second a declaration of y of type int, or is it a parameter of type y?
May 22 2006
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 22 May 2006 19:30:45 -0700, Walter Bright wrote:

 Dave wrote:
 Couldn't find this in the archive, so I don't know if it's been discussed
 before.
 
 One of the things I like about Pascal is that you can specify function
arguments
 of the same type w/o repeating the type. So, in D we could:
 
 int foo(int x, y, z) // y and z are type int
 {}
 
 int bar(int x = 1, y = 2, z = 3) // y and z are type int
 {}
 
 void baz(int x, y, double d, f = 3.14159)  // y is an int, f is a double
 {}
 
 Thoughts?
It has grammatical ambiguities. Consider: int foo(int x, y); Is the second a declaration of y of type int, or is it a parameter of type y?
Aside from the suggested syntax, what do you think about the concept, Walter? -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 23/05/2006 12:46:33 PM
May 22 2006
parent Walter Bright <newshound digitalmars.com> writes:
Derek Parnell wrote:
 Aside from the suggested syntax, what do you think about the concept,
 Walter? 
I think that getting rid of redundancy is a good idea in general. I dislike having to type things in twice.
May 22 2006
prev sibling parent reply Dave <Dave_member pathlink.com> writes:
Walter Bright wrote:
 Dave wrote:
 Couldn't find this in the archive, so I don't know if it's been discussed
 before.

 One of the things I like about Pascal is that you can specify function 
 arguments
 of the same type w/o repeating the type. So, in D we could:

 int foo(int x, y, z) // y and z are type int
 {}

 int bar(int x = 1, y = 2, z = 3) // y and z are type int
 {}

 void baz(int x, y, double d, f = 3.14159)  // y is an int, f is a double
 {}

 Thoughts?
It has grammatical ambiguities. Consider: int foo(int x, y); Is the second a declaration of y of type int, or is it a parameter of type y?
(Red faced) Of course. Thanks, - Dave
May 22 2006
parent reply Bill Baxter <Bill_member pathlink.com> writes:
In article <e4tv46$2007$1 digitaldaemon.com>, Dave says...
Walter Bright wrote:
 Dave wrote:
 Couldn't find this in the archive, so I don't know if it's been discussed
 before.

 One of the things I like about Pascal is that you can specify function 
 arguments
 of the same type w/o repeating the type. So, in D we could:

 int foo(int x, y, z) // y and z are type int
 {}

 int bar(int x = 1, y = 2, z = 3) // y and z are type int
 {}

 void baz(int x, y, double d, f = 3.14159)  // y is an int, f is a double
 {}

 Thoughts?
It has grammatical ambiguities. Consider: int foo(int x, y); Is the second a declaration of y of type int, or is it a parameter of type y?
(Red faced) Of course. Thanks, - Dave
So what about a semi colon or colon or something instead of the comma? int foo(int x ; y, float z); int foo(int x : y : z); Course, then the syntax doesn't resemble the declaration syntax any more. Personally, I think arguments with types but not names is bad style anyway. The argument name gives you a clue as to what that parameter is supposed to be for. If you see int foo(int,int,int) there's no way to tell what those ints are for, and you can't document them either because there's no parameter name to refer to in the documentation. --bill
May 24 2006
next sibling parent reply Bill Baxter <Bill_member pathlink.com> writes:
In article <e53f58$15dc$1 digitaldaemon.com>, Bill Baxter says...
In article <e4tv46$2007$1 digitaldaemon.com>, Dave says...
Walter Bright wrote:
 It has grammatical ambiguities. Consider:
 
 int foo(int x, y);
 
 Is the second a declaration of y of type int, or is it a parameter of 
 type y?
So what about a semi colon or colon or something instead of the comma? int foo(int x ; y, float z); int foo(int x : y : z);
Or even a space for that matter: int foo(int x y z, real z); But when you say there's a grammatical ambiguity, can there actually be variables with the same names as types? Isn't that against the rules? So it's just a low level syntactical ambiguity you're talking about, right? At the time you see the ', real' there's ambiguity but only until you check if 'real' is a type or not. In other words int foo(int x, real) has to be one int arg, one real arg, and not two int args, one named 'x', the other named 'real'. Right? Bill
May 25 2006
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Bill Baxter wrote:
 [snip]
 
 But when you say there's a grammatical ambiguity, can there actually be
 variables with the same names as types?  Isn't that against the rules?  So it's
 just a low level syntactical ambiguity you're talking about, right?  At the
time
 you see the ', real' there's ambiguity but only until you check if 'real' is a
 type or not.
That's the problem. That can't be done in a context-free grammar[1]. This means that the stages of parsing and semantic analysis aren't independent, which is a big deal to Walter (and for good reason). It's a good idea, and I like it; but you need to find a way to express it unambiguously. -- Daniel Keep [1] Note: this is based on my somewhat shaky understanding of what "context free grammar" means. I could very well be wrong, but my intuition tells me otherwise. -- v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 25 2006
parent Walter Bright <newshound digitalmars.com> writes:
Daniel Keep wrote:
 [1] Note: this is based on my somewhat shaky understanding of what
 "context free grammar" means.  I could very well be wrong, but my
 intuition tells me otherwise.
In this instance, you're right.
May 25 2006
prev sibling parent Roberto Mariottini <Roberto_member pathlink.com> writes:
In article <e53f58$15dc$1 digitaldaemon.com>, Bill Baxter says...

So what about a semi colon or colon or something instead of the comma?

int foo(int x ; y, float z);
int foo(int x : y : z);
My old proposal was to use a colon to disambiguate declarations. So int foo (int x, int y, int z, float value); could also be written as: int foo (int : x, y, z; float value); My proposal went further, forcing to use a colon for multiple declarations: int a; // OK int b, c; // error int : d; // also OK int : e, f, g; // OK Ciao --- http://www.mariottini.net/roberto/
May 26 2006