Using in as a parameter qualifier

Jonathan M Davis jmdavisProg at gmx.com
Fri May 31 18:16:21 PDT 2013


On Saturday, June 01, 2013 05:15:22 Shriramana Sharma wrote:
> On Sat, Jun 1, 2013 at 5:11 AM, Shriramana Sharma <samjnaa at gmail.com> wrote:
> > So what is the syntax that accepts rvalues and yet does not make a
> > copy of the input object? I mean, as Ali says OK profiling is
> > preferable to find out where exactly passing by value is more
> > efficient than passing by const ref, but I'd like to know anyway what
> > is the syntax available for this so at least until I get my D legs, I
> > can continue some of my practices from C++ without much detriment.
> 
> And again, sorry if I'm being dumb, but is the proposed inout syntax
> intended to fix this problem or some other problem?
> http://d.puremagic.com/issues/show_bug.cgi?id=3748 is marked as
> resolved fixed from back in 2011, but if so why did DConf *this year*
> have a talk on this issue?!

inout is completely unrelated. It has to do with having a function which 
accepts mutable, const, and immutable and returns the same level mutability 
without having to duplicate your function. You don't want to have to have 
something like

int[] foo(int[] bar)
{
 ... do something
 return bar[i .. j];
}

const(int)[] foo(const(int)[] bar)
{
 ... do something
 return bar[i .. j];
}


immutable(int)[] foo(immutable(int)[] bar)
{
 ... do something
 return bar[i .. j];
}

Without inout, you're forced to do this kind of code duplication when you want 
to accept multiple types of mutability and return the same mutability. You 
could just accept const, but then you'd have to return const rather than the 
original type which could also have been mutable or immutable.

const(int)[] foo(const(int)[] bar)
{
 ... do something
 return bar[i .. j];
}

inout solves that problem:

inout(int)[] foo(inout(int)[] bar)
{
 ... do something
 return bar[i .. j];
}

You only have to write the function once, and it works with all 3 levels of 
mutability.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list