nested inout return type

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 14 07:47:11 PDT 2016


On 6/14/16 9:22 AM, Simon Bürger wrote:
> On Tuesday, 14 June 2016 at 01:50:17 UTC, Era Scarecrow wrote:
>> On Monday, 13 June 2016 at 23:51:40 UTC, Era Scarecrow wrote:
>>>      inout(Slice) opSlice(size_t a, size_t b) inout
>>>      {
>>>          return cast(inout(Slice)) Slice(ptr+a, b-a);
>>>      }
>>
>>  Seems the pointer has to be force-cast back to a normal pointer so
>> the constructor can work. (Because the function is inout, ptr becomes
>> inout(T*) )
>>
>>      return cast(inout(Slice)) Slice(cast(T*)ptr+a, b-a);

Better: inout(Slice)(ptr+a, b-a);

> Then instead of Slice!(const(T)) one would use const(Slice!T). Then
> there is no analogue of the following:
>
> const(T)[] s = ...
> s = s[5..7]

Yes, this is a missing piece of the language. In order to use custom 
types, you must give up tail modifiers.

> which is quite common when parsing strings for example. Still, might be
> the cleanest approach. However I found another solution for now without
> using any "inout":
>
> * only do one mutable version of opSlice
> * add implicit cast (using "alias this") for const(Slice!T) ->
> Slice!(const(T)).

Interesting, but unfortunately, the compiler isn't eager about this 
conversion. auto x = s[5 .. 7] isn't going to give you a 
Slice!(const(T)), like an array would. But I like the idea.

And of course, there is the issue of not allowing structs with inout 
members. So really you need to triplicate the function for all the 
modifiers. And of course, alias this can only be used once...

-Steve


More information about the Digitalmars-d-learn mailing list