opDollar and infinite slices on infinite ranges

Simen kjaeraas simen.kjaras at gmail.com
Wed Jul 21 12:47:33 PDT 2010


Philippe Sigaud <philippe.sigaud at gmail.com> wrote:

> On Sat, Jul 17, 2010 at 22:01, Philippe Sigaud  
> <philippe.sigaud at gmail.com>wrote:
>
>>
>> My google-fu is weak, I cannot find any indication on opDollar and how  
>> to
>> define it, for D2. So, maybe someone here can help me? Can someone  
>> points
>> me
>> to some documentation?
>>
>>
> No, no one knows something about opDollar and how to define it?
>
> Pretty please?

I wrote this indexing struct. Clearly not perfect, but it sorta does the  
right things:

struct Index {
     long delegate( long ) dg;

     long opCall( long value ) {
         return dg( value );
     }

     Index opUnary( string op )( ) {
         dg = ( long value ) {
                 mixin( " auto tmp = dg( value ); tmp" ~ op ~ "; return  
tmp;" );
             };
         return this;
     }

     Index opBinary( string op )( long n ) const {
         Index result;
         result.dg = ( long value ) {
                 mixin( "return dg( value ) " ~ op ~ " n;" );
             };
         return result;
     }

     Index opOpAssign( string op )( long n ) {
         dg = ( long value ) {
                 mixin( "return dg( value ) " ~ op ~ " n;" );
             };
         return this;
     }
}

Index __dollar( ) {
     Index result;
     result.dg = ( long value ){ return value; };
     return result;
}

To use, make opSlice take an Index parameter, and call its opCall with the  
length
of the container. Wish there was a way to make late-bound templates in D,  
as that
could allow for different value types depending on usage.
Hm, maybe there is a way, now that I think of it. I shall have to check in  
the future.

--
Simen


More information about the Digitalmars-d mailing list