function argument 'shorthand'

James Dunne james.jdunne at gmail.com
Mon May 22 07:52:25 PDT 2006


Derek Parnell wrote:
> On Sat, 20 May 2006 15:00:29 +1000, Dave <Dave_member at 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



More information about the Digitalmars-d mailing list