Delegator

Jacob Carlborg doob at me.com
Sat Mar 3 10:18:36 PST 2012


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".

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list