April 23, 2004 Re: Multiple value returns in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tydr Schnubbis | On Thu, 22 Apr 2004 21:35:55 +0200, Tydr Schnubbis wrote: > Derek Parnell wrote: > > Ok. Not wanting to change D very much, I'd suggest that the current >> function signature syntax be used. eg.... >> >> void foo(int A, int B, out int C, out int D) >> >> and then inside the function, again with no changes, the coder would just use normal assignments to the 'out' arguments. >> >> The changes would be only involved in how such a function is called. Rather than use the existing syntax (though this would still be available), the coder could call the function thus (and this is just a suggestion so far) ... >> >> #(FldA, FldX) = foo( Data_1, Data_2); >> >> and the compiler would translate this as if the coder had written ... >> >> foo(Data_1, Data_2, FldA, FldX); >> >> The compiler maps the arguments by using the function signature from left to right, when it gets an 'out' argument it uses the next parameter from the #(...) list otherwise it uses the parameter from the function call list. >> >> The benefit would be clearer (explicit) code that the routine is modifying data. This helps the reader of the code who can then see this without having to search for the function signature somewhere. >> > > Wouldn't you get most of that effect just by requiring the caller to prefix output arguments with 'out' when calling the function, like this? > > // declare > int foo(int a, out char[] s); > > // call > char[] s; > int x; > x = foo(5, out s); // 'out' required > > I think maybe C# does it like this. > But there's probably some reason unknown to me why this isn't done like > this already... > > Tor Yeah, that would work too, given that I'm trying to make the act of reading code less error prone. This might even catch some sloppy code writing habits too. -- Derek 23/Apr/04 10:00:48 AM |
Copyright © 1999-2021 by the D Language Foundation