isInfinite isInadequate

monarch_dodra monarchdodra at gmail.com
Tue Mar 12 04:44:22 PDT 2013


On Tuesday, 12 March 2013 at 11:24:00 UTC, Dmitry Olshansky wrote:
> 12-Mar-2013 14:49, monarch_dodra пишет:
>
>> Up until not so long ago, it was not possible to slice 
>> infinite ranges.
>> It is now possible. Unfortunatly, because RA ranges with 
>> slicing don't
>> guarantee that r[0 .. $] is legal, things are still 
>> complicated in
>> template code.
>
> I thought it goes in opposite direction - slicing of Infinite 
> range works only for ..$ slice. The chief new requirement of 
> slicing is self-assign ability.

Its... complicated.

To be exact: hasSlicing only requires that:
//--------
         static if(isInfinite!R)
             typeof(take(r, 1)) s = r[1 .. 2];
         else
         {
             static assert(is(typeof(r[1 .. 2]) == R));
             R s = r[1 .. 2];
         }
         //minor tests here
//--------

Which means that if you are going to slice an RA, make sure it is 
finite before back-assigning.

 From there, *if* r[0 .. $] is legal, *then* extra checks are made:
//--------
         static if(is(typeof(r[0 .. $])))
         {
             static assert(is(typeof(r[0 .. $]) == R));
             //minor tests here
         }
//--------

*Ideally*, for finite RA ranges, we would want to enforce that [0 
.. $] MUST be legal. This would be breaking change however. We'd 
want the compiler to auto generate opDollar, which would make 
things simpler for everyone:
http://d.puremagic.com/issues/show_bug.cgi?id=7177
The only reason this isn't done yet (AFAIK) is because:
* Support for opDollar is still relativelly new.
* exact details are not fully thought out yet.

As for infinite ranges, ditto. Basically, once you've implemented 
"slice to end", normal slicing is also trivial.

However, today, this is not the case, since it's breaking change.

If you want to call "r[0 .. $]", you have to jump through hoops 
to know if it is legal code.

...

Come to think about it, with today's "deprecated features are on 
by default", I think it would be possible to activate this new 
requirement without breaking any code. Those that activate -w 
would then notice their range needs change to meet the new 
hasSlicing requirements. traits + deprecation is funny business.

@jmdavis: What do you think?


More information about the Digitalmars-d mailing list