So what does (inout int = 0) do?

Kenji Hara via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 15 03:50:53 PDT 2016


2016-04-15 14:38 GMT+09:00 Andrei Alexandrescu via Digitalmars-d <
digitalmars-d at puremagic.com>:

> On 04/15/2016 12:23 AM, Jonathan M Davis via Digitalmars-d wrote:
>
>> 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
>>
>
> I think we should deprecate inout. For real. It costs way too much for
> what it does. For all I can tell most of D's proponents don't know how it
> works. -- Andrei
>


You should recall the history of inout.
http://wiki.dlang.org/DIP2

At first, It has designed to temporarily squash mutable/const/immutable
qualifiers on function argument inside function body. Therefore when inout
qualifier appears in function return type, but doesn't on parameter types,
we defined it an error.

However, I concluded that we can remove the restriction. When an inout
object arises from thin air, no one holds the qualifier of its real
instance. It means, the returned inout object can be converted to arbitrary
qualifiers without type system breaking.

Two years ago I opened an enhancement issue:
https://issues.dlang.org/show_bug.cgi?id=13006

And posted a pull request.
https://github.com/D-Programming-Language/dmd/pull/3740

Kenji Hara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20160415/3419fef2/attachment.html>


More information about the Digitalmars-d mailing list