Bypassing const with a union

Steven Schveighoffer schveiguy at yahoo.com
Mon Jun 25 08:05:21 PDT 2012


On Fri, 01 Jun 2012 18:51:54 -0400, Era Scarecrow <rtcvb32 at yahoo.com>  
wrote:

> On Friday, 1 June 2012 at 21:15:18 UTC, Ali Çehreli wrote:
>> Don't forget inout, which seems to be working at least in this case:
>
>   I'd given up on trying to use inout, being as it's confusing to try  
> and use, when I try to use it it, it always complains. Perhaps better  
> documentation/examples or I gotta look at it all over again. To my  
> understanding you had to have inout in the signature and as a input  
> parameter (which no input arguments never fit). Hmm..

inout is not completely fleshed out yet.  It's turning out to be quite a  
complex problem to solve, but it's getting there.

>
>> import std.traits;
>> import std.stdio;
>>
>> struct S {
>>     size_t[] array;
>>
>>     void length(size_t length) @property
>>     {
>>         array.length = length;
>>     }
>>
>>     inout(S) copy() inout {
>>         return this;
>>     }
>>
>>     inout(S) opSlice(int s, int e) inout {
>>         Unqual!S sl;
>>         sl.array = (cast(Unqual!S)this).array[s .. e];
>>         return cast(inout(S))sl;
>>     }
>> }

I think this will work for slicing:

inout(S) opSlice(int s, int e) inout {
    return inout(S)(array[s..e]);
}

I use this quite a bit in dcollections.  This seems more attractive to me  
to any other solution, especially if casting is involved.

-Steve


More information about the Digitalmars-d-learn mailing list