Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 20, 2006 function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
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 21, 2006 Re: function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dave | 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 22, 2006 Re: function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | 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 Re: function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne | 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 Re: function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dave | 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 23, 2006 Re: function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dave | 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 23, 2006 Re: function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 23, 2006 Re: function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 23, 2006 Re: function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | 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 25, 2006 Re: function argument 'shorthand' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dave | 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 |
Copyright © 1999-2021 by the D Language Foundation