Declaring Ref Variables Inside Function Calls
dsimcha
dsimcha at yahoo.com
Mon Mar 30 20:40:55 PDT 2009
== Quote from Denis Koroskin (2korden at gmail.com)'s article
> 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?).
Nothing wrong with auto, but you need to specify something, even if it's just
auto, to make it clear that you're declaring a type.
> 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.
Here's an example of where it would be a useful piece of sugar, given an unpack
function that I was playing with:
// How it works now:
uint foo;
string bar;
unpack(foo, bar) = someFunction();
// vs. how I want it to work:
unpack(auto foo, auto bar) = someFunction();
More information about the Digitalmars-d
mailing list