Delegator

bioinfornatics bioinfornatics at fedoraproject.org
Sat Mar 3 15:41:03 PST 2012


Le samedi 03 mars 2012 à 19:18 +0100, Jacob Carlborg a écrit :
> On 2012-03-03 17:50, bioinfornatics wrote:
> > Le samedi 03 mars 2012 à 17:42 +0100, bioinfornatics a écrit :
> >> hi,
> >> In ruby we can delegate some method. by example:
> >> ---- Ruby ----
> >> class MineArray
> >>    include Forwardable
> >>    def_delegators: @array, :[], :[]=, :each_with_index, :length
> >>
> >>    def initialize( array )
> >>      @array = array
> >>    end
> >> end
> >> -------------
> >>
> >> this code delegate opIndexAssign opIndex, length ... attribute to his
> >> member.
> >>
> >> This save time, bug and line. You do not to have to write a code as:
> >> void opIndexAssign( size_t index, T item){
> >> 	array[index] = item;
> >> }
> >>
> >> thanks
> >>
> > I miss the question ^^
> >
> > can w do same in D ?
> >
> > thanks
> 
> I would say "yes", but not with the same pretty syntax. Something like this:
> 
> class MineArray
> {
>      mixin delegates!(array, "opIndexAssign", "opIndex");
> }
> 
> Just implement "delegates" to generate the given functions and forward 
> them to "array".
> 


I have try to do a mixin template but i fail
------------------ D Code --------------------
import std.string;
import std.stdio;
import std.range;

mixin template arrayDelegator( alias instance, methods... ){
    string result;
   static if( methods.length > 0 ){
        static if( "opIndexAssign" ){
            result ~="
    void opIndexAssign( size_t index, " ~
ElementEncodingType!(typeof(instance)) ~ " item ){ 
        array[index] = item;
    }
                    ";
        }
        static else if( "opIndex" ){
            result ~="
    " ~ ElementEncodingType!(typeof(instance)) ~ " opIndex( size_t index
){
        return instance[index];
    }
                    ";
        }
        static else if( "length" ){
            result ~="
    @property size_t length(){
        return instance.length;
    }
                    ";
        }
        static else
            throw new Exception( "Unknown methods: "~ method );
        static if( methods.length > 2 )
            arrayDelegator!( instance, methods[1 .. $ ] );
    } 
    mixin(result);
}

class Container{
    size_t[] array;

    mixin arrayDelegator!(array, "opIndexAssign", "opIndex", "length");
    
    this( size_t[] a ){
        array = a:
    }

}

void main( string[] args ){
    Container c = new Container( [0u, 1u, 2u, 3u] );
    writeln( c[2] );
    c[2] = 4u;
    writeln( c[2] );
}
---------------------------------------------------------

$ ldc2 delegator.d
delegator.d(9): no identifier for declarator result
delegator.d(9): semicolon expected, not '~='
delegator.d(9): Declaration expected, not '~='
delegator.d(15): Declaration expected, not 'else'
delegator.d(22): Declaration expected, not 'else'
delegator.d(29): Declaration expected, not 'else'
delegator.d(32): no identifier for declarator
arrayDelegator!(instance,methods[1 .. __dollar])
delegator.d(33): unrecognized declaration





More information about the Digitalmars-d-learn mailing list