So what does (inout int = 0) do?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 14 21:23:29 PDT 2016


On Thursday, April 14, 2016 23:10:12 Andrei Alexandrescu via Digitalmars-d 
wrote:
> Consider:
>
> https://github.com/D-Programming-Language/phobos/blob/master/std/range/primi
> tives.d#L152
>
> There is no explanation to it in the source code, and the line blames to
> https://github.com/D-Programming-Language/phobos/pull/2661 (irrelevant).
>
> Commenting it out yields a number of unittest compilation errors,
> neither informative about the root of the problem and indicative as to
> how the parameter solves it.
>
> There are two issues here:
>
> 1. Once a problem/solution pair of this degree of subtlety crops up, we
> need to convince ourselves that that's sensible. If we deem it not so,
> we look into improving the language to make the problem disappear.
>
> 2. There needs to be documentation for people working on the standard
> library so they don't need to waste time on their own discovery process.
>
> We want Phobos to be beautiful, a prime example of good D code.
> Admittedly, it also needs to be very general and efficient, which
> sometimes gets in the way. But we cannot afford an accumulation of mad
> tricks to obscure the large design.

IIRC, the problem has to do with ranges of inout elements working correctly,
which gets really funky, because inout is a temporary thing and not a
full-on type constructor/qualifier. I believe that Kenji is the one that
implemented the fix, and I think that he explained it in the newsgroup at
some point. Certainly, there have been a few times that it's come up in
D.Learn when folks ask what the heck it is, so there should be a few posts
floating around with an explanation. This is the only useful post that I
could find in a quick search though:

http://forum.dlang.org/post/mh68p8$2p56$1@digitalmars.com

inout attempts to solve a very real problem, but it does seem to be
surprisingly hard to understand and use prooperly even though it's
theoretically simple. And this bit with ranges is a quirk that I think very
few people understand and remember. I usually forget exactly what it does
when it comes up and have to try and dig through the newsgroup archives for
a previous discussion on it.

- Jonathan M Davis



More information about the Digitalmars-d mailing list