nested inout return type

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 14 12:48:06 PDT 2016


On 6/14/16 3:38 PM, Era Scarecrow wrote:
> On Tuesday, 14 June 2016 at 14:47:11 UTC, Steven Schveighoffer wrote:
>>> On Tuesday, 14 June 2016 at 01:50:17 UTC, Era Scarecrow wrote:
>>>>      return cast(inout(Slice)) Slice(cast(T*)ptr+a, b-a);
>>
>> Better: inout(Slice)(ptr+a, b-a);
>
>  Of course... My amateur D-fu skills show themselves. cast() const()
> immutable() are the same, why not inout? Hmmm...

It's mostly akin to immutable (which would require similar syntax). It's 
because once constructed, inout-flavored wrappers cannot change anything 
internally. So you have to do it all at once.

const is different, because you can construct mutable, and then simply 
cast to const when you feel like it.

But of course, if the components you are using to construct are already 
flavored, you have no choice. All-at-once construction is the only way 
without casting.

>  I feel like a tutorial for common problems and solutions should be
> present for C, C++ & D. Then again maybe that's some cookbooks that I
> haven't purchased/read yet.

For a long time I have been working on an article to talk about the 
quirks/tricks of inout. I gave a talk at dconf, but there are more 
things to show than are possible in a spoken talk.

I honestly think the best place to go figure these things out is 
stackoverflow (or just the internet in general). Whenever I have a 
technical problem I can't figure out (or am too lazy to diagnose 
myself), I search and SO usually gives me an answer :)

-Steve


More information about the Digitalmars-d-learn mailing list