digitalmars.D - function argument 'shorthand'
- Dave (11/11) May 19 2006 Couldn't find this in the archive, so I don't know if it's been discusse...
- Derek Parnell (9/21) May 21 2006 It's not a good idea because it makes things too easy for coders to writ...
- James Dunne (20/50) May 22 2006 Pascal syntax:
- Dave (6/55) May 22 2006 I didn't want to take it that far :). Just far enough that "following
- Johan Granberg (3/8) May 22 2006 I agree this is a feature that would save a lot of typing and would bee
- Walter Bright (5/21) May 22 2006 It has grammatical ambiguities. Consider:
- Derek Parnell (9/33) May 22 2006 Aside from the suggested syntax, what do you think about the concept,
- Walter Bright (3/5) May 22 2006 I think that getting rid of redundancy is a good idea in general. I
- Dave (4/29) May 22 2006 (Red faced) Of course.
- Bill Baxter (12/41) May 24 2006 So what about a semi colon or colon or something instead of the comma?
- Bill Baxter (13/26) May 25 2006 Or even a space for that matter:
- Daniel Keep (13/20) May 25 2006 That's the problem. That can't be done in a context-free grammar[1].
- Walter Bright (2/5) May 25 2006 In this instance, you're right.
- Roberto Mariottini (13/16) May 26 2006 My old proposal was to use a colon to disambiguate declarations. So
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
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
Derek Parnell wrote:On Sat, 20 May 2006 15:00:29 +1000, Dave <Dave_member pathlink.com> wrote: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 DunneCouldn'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})
May 22 2006
James Dunne wrote:Derek Parnell 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>On Sat, 20 May 2006 15:00:29 +1000, Dave <Dave_member pathlink.com> wrote:Pascal syntax: function foo(x,y,z : int) : int procedure baz(x,y : int, d, f : double = 3.14159)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})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
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
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
On Mon, 22 May 2006 19:30:45 -0700, Walter Bright wrote:Dave wrote: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 PMCouldn'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
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
Walter Bright wrote:Dave wrote:(Red faced) Of course. Thanks, - DaveCouldn'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
In article <e4tv46$2007$1 digitaldaemon.com>, Dave says...Walter Bright wrote: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. --billDave wrote:(Red faced) Of course. Thanks, - DaveCouldn'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 24 2006
In article <e53f58$15dc$1 digitaldaemon.com>, Bill Baxter says...In article <e4tv46$2007$1 digitaldaemon.com>, Dave says...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? BillWalter Bright wrote: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);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 25 2006
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
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
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