Declaring Ref Variables Inside Function Calls

Denis Koroskin 2korden at gmail.com
Mon Mar 30 20:25:05 PDT 2009


On Tue, 31 Mar 2009 06:46:32 +0400, dsimcha <dsimcha at yahoo.com> wrote:

> At times, like when trying to write a syntactically sweet tuple  
> unpacker, I've
> wanted to be able to declare a variable that will be passed by reference  
> to a
> function inside the function call.  For example:
>
> void doStuff(out uint num) {  // Could also be ref uint num.
>     num = 666;
> }
>
> import std.stdio;
>
> void main() {
>     doStuff(uint foo);  // Declare foo as uint, passes it to doStuff.
>     writeln(foo);  // Prints 666.
> }
>
> Is it feasible, at least in principle, to allow this, or would this  
> create
> issues with parsing, odd ambiguities that I haven't thought of, etc.?
>


It seems loogically that "foo" should die right after doStuff returns,  
because its scope is limited to parens.
It's also redundant to specify type of foo, it can be deduced from  
function signature (auto foo?).

That said, I don't think there is a need for something like this.  
Certainly no speedup and little clarity compared to declaring variable  
right before invoking doStuff.



More information about the Digitalmars-d mailing list