www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Delegator

reply bioinfornatics <bioinfornatics fedoraproject.org> writes:
hi,
In ruby we can delegate some method. by example:
---- Ruby ----
class MineArray
  include Forwardable
  def_delegators:  array, :[], :[]=3D, :each_with_index, :length

  def initialize( array )
     array =3D 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] =3D item;
}

thanks
Mar 03 2012
next sibling parent reply bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le samedi 03 mars 2012 =C3=A0 17:42 +0100, bioinfornatics a =C3=A9crit :
 hi,
 In ruby we can delegate some method. by example:
 ---- Ruby ----
 class MineArray
   include Forwardable
   def_delegators:  array, :[], :[]=3D, :each_with_index, :length
=20
   def initialize( array )
      array =3D array
   end
 end
 -------------
=20
 this code delegate opIndexAssign opIndex, length ... attribute to his
 member.
=20
 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] =3D item;
 }
=20
 thanks
=20

can w do same in D ? thanks
Mar 03 2012
parent Jacob Carlborg <doob me.com> writes:
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

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
Mar 03 2012
prev sibling next sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le samedi 03 mars 2012 =C3=A0 19:18 +0100, Jacob Carlborg a =C3=A9crit :
 On 2012-03-03 17:50, bioinfornatics wrote:
 Le samedi 03 mars 2012 =C3=A0 17:42 +0100, bioinfornatics a =C3=A9crit =


 hi,
 In ruby we can delegate some method. by example:
 ---- Ruby ----
 class MineArray
    include Forwardable
    def_delegators:  array, :[], :[]=3D, :each_with_index, :length

    def initialize( array )
       array =3D 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] =3D item;
 }

 thanks

can w do same in D ? thanks

I would say "yes", but not with the same pretty syntax. Something like th=

=20
 class MineArray
 {
      mixin delegates!(array, "opIndexAssign", "opIndex");
 }
=20
 Just implement "delegates" to generate the given functions and forward=

 them to "array".
=20

they are a way to create a decorator as delgator for able to this pretty ?
Mar 03 2012
prev sibling next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Saturday, 3 March 2012 at 16:50:45 UTC, bioinfornatics wrote:
 can w do same in D ?

alias this does that, although it does for all unknown methods, not specific ones: struct A { int[] data; alias data this; } A a; a[0] = 10; // this works like a.data[0] = 10; alias this also lets you pass the struct when the member is expected: void somethingWithArray(int[] arr) {} A a; somethingWithArray(a); // works, passes a.data automatically
Mar 03 2012
prev sibling next sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le samedi 03 mars 2012 =C3=A0 19:45 +0100, Adam D. Ruppe a =C3=A9crit :
 On Saturday, 3 March 2012 at 16:50:45 UTC, bioinfornatics wrote:
 can w do same in D ?

alias this does that, although it does for all unknown methods, not specific ones: =20 struct A { int[] data; alias data this; } =20 A a; =20 a[0] =3D 10; // this works like a.data[0] =3D 10; =20 =20 alias this also lets you pass the struct when the member is expected: =20 void somethingWithArray(int[] arr) {} =20 A a; somethingWithArray(a); // works, passes a.data automatically

But when you have already a ctor ( class ) can you alias this ?
Mar 03 2012
prev sibling next sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le samedi 03 mars 2012 =C3=A0 19:18 +0100, Jacob Carlborg a =C3=A9crit :
 On 2012-03-03 17:50, bioinfornatics wrote:
 Le samedi 03 mars 2012 =C3=A0 17:42 +0100, bioinfornatics a =C3=A9crit =


 hi,
 In ruby we can delegate some method. by example:
 ---- Ruby ----
 class MineArray
    include Forwardable
    def_delegators:  array, :[], :[]=3D, :each_with_index, :length

    def initialize( array )
       array =3D 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] =3D item;
 }

 thanks

can w do same in D ? thanks

I would say "yes", but not with the same pretty syntax. Something like th=

=20
 class MineArray
 {
      mixin delegates!(array, "opIndexAssign", "opIndex");
 }
=20
 Just implement "delegates" to generate the given functions and forward=

 them to "array".
=20

I found this way verys interestiong. Could you put plsease a shorter implementation of delegates?
Mar 03 2012
prev sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le samedi 03 mars 2012 =C3=A0 19:18 +0100, Jacob Carlborg a =C3=A9crit :
 On 2012-03-03 17:50, bioinfornatics wrote:
 Le samedi 03 mars 2012 =C3=A0 17:42 +0100, bioinfornatics a =C3=A9crit =


 hi,
 In ruby we can delegate some method. by example:
 ---- Ruby ----
 class MineArray
    include Forwardable
    def_delegators:  array, :[], :[]=3D, :each_with_index, :length

    def initialize( array )
       array =3D 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] =3D item;
 }

 thanks

can w do same in D ? thanks

I would say "yes", but not with the same pretty syntax. Something like th=

=20
 class MineArray
 {
      mixin delegates!(array, "opIndexAssign", "opIndex");
 }
=20
 Just implement "delegates" to generate the given functions and forward=

 them to "array".
=20

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 ~=3D" void opIndexAssign( size_t index, " ~ ElementEncodingType!(typeof(instance)) ~ " item ){=20 array[index] =3D item; } "; } static else if( "opIndex" ){ result ~=3D" " ~ ElementEncodingType!(typeof(instance)) ~ " opIndex( size_t index ){ return instance[index]; } "; } static else if( "length" ){ result ~=3D" property size_t length(){ return instance.length; } "; } static else throw new Exception( "Unknown methods: "~ method ); static if( methods.length > 2 ) arrayDelegator!( instance, methods[1 .. $ ] ); }=20 mixin(result); } class Container{ size_t[] array; mixin arrayDelegator!(array, "opIndexAssign", "opIndex", "length"); =20 this( size_t[] a ){ array =3D a: } } void main( string[] args ){ Container c =3D new Container( [0u, 1u, 2u, 3u] ); writeln( c[2] ); c[2] =3D 4u; writeln( c[2] ); } --------------------------------------------------------- $ ldc2 delegator.d delegator.d(9): no identifier for declarator result delegator.d(9): semicolon expected, not '~=3D' delegator.d(9): Declaration expected, not '~=3D' 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
Mar 03 2012