www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - myClass.add(something)(otherthings)(thisToo);

reply tsalm <tsalm free.fr> writes:
Hello,

How to implement an object that can do this :
myClass.add(something)(otherthings)(thisToo);

Is it possible ?

TIA,
TSalm
Dec 09 2008
next sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
tsalm wrote:
 Hello,
 
 How to implement an object that can do this :
 myClass.add(something)(otherthings)(thisToo);
 
 Is it possible ?
 
 TIA,
 TSalm
Something like this might work: class MyClass{ int[] stuff; alias add opCall; MyClass add(int k){ stuff ~= k; return this; } }
Dec 09 2008
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Wed, 10 Dec 2008 02:40:47 +0300, tsalm <tsalm free.fr> wrote:

 Hello,

 How to implement an object that can do this :
 myClass.add(something)(otherthings)(thisToo);

 Is it possible ?

 TIA,
 TSalm
Yes: import std.stdio; class MyClass { class MyClassAdder { MyClassAdder opCall(Foo foo) { _add(foo); return this; } } class MyClassRemover { MyClassRemover opCall(Foo foo) { _remove(foo); return this; } } this() { _adder = new MyClassAdder(); _remover = new MyClassRemover(); } MyClassAdder add(Foo foo) { _add(foo); return _adder; } MyClassRemover remove(Foo foo) { _remove(foo); return _remover; } private void _add(Foo foo) { writefln("added"); } private void _remove(Foo foo) { writefln("removed"); } private MyClassAdder _adder; private MyClassRemover _remover; } class Foo { } void main() { MyClass myClass = new MyClass(); myClass.add(null)(null)(null); myClass.remove(null)(null)(null); }
Dec 09 2008
prev sibling parent reply BCS <ao pathlink.com> writes:
Reply to TSalm,

 Hello,
 
 How to implement an object that can do this :
 myClass.add(something)(otherthings)(thisToo);
 Is it possible ?
 
 TIA,
 TSalm
if you don't mind dropping the )( class C { final void add(T...)(T t) { foreach(int i,_;T) _add(t[i]); } //..... } (new C).add(something, otherthings, thisToo);
Dec 09 2008
parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Tue, Dec 9, 2008 at 7:00 PM, BCS <ao pathlink.com> wrote:
 class C
 {
   final void add(T...)(T t)
   {
       foreach(int i,_;T)
           _add(t[i]);
   }
   //.....
 }


 (new C).add(something, otherthings, thisToo);
If all the params are the same type, typesafe variadics are a more efficient/less code-bloaty way to do it. void add(int[] things...) { foreach(thing; things) _add(thing); }
Dec 09 2008
parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Wed, 10 Dec 2008 03:24:48 +0300, Jarrett Billingsley
<jarrett.billingsley gmail.com> wrote:

 On Tue, Dec 9, 2008 at 7:00 PM, BCS <ao pathlink.com> wrote:
 class C
 {
   final void add(T...)(T t)
   {
       foreach(int i,_;T)
           _add(t[i]);
   }
   //.....
 }


 (new C).add(something, otherthings, thisToo);
If all the params are the same type, typesafe variadics are a more efficient/less code-bloaty way to do it. void add(int[] things...) { foreach(thing; things) _add(thing); }
*And* allows overriding them!
Dec 09 2008
next sibling parent tsalm <tsalm free.fr> writes:
Le Wed, 10 Dec 2008 03:16:49 +0100, Denis Koroskin <2korden gmail.com> a  
écrit:

 On Wed, 10 Dec 2008 03:24:48 +0300, Jarrett Billingsley  
 <jarrett.billingsley gmail.com> wrote:

 On Tue, Dec 9, 2008 at 7:00 PM, BCS <ao pathlink.com> wrote:
 class C
 {
   final void add(T...)(T t)
   {
       foreach(int i,_;T)
           _add(t[i]);
   }
   //.....
 }


 (new C).add(something, otherthings, thisToo);
If all the params are the same type, typesafe variadics are a more efficient/less code-bloaty way to do it. void add(int[] things...) { foreach(thing; things) _add(thing); }
*And* allows overriding them!
Thanks you all !
Dec 09 2008
prev sibling parent reply BCS <ao pathlink.com> writes:
Reply to Denis,

 On Wed, 10 Dec 2008 03:24:48 +0300, Jarrett Billingsley
 <jarrett.billingsley gmail.com> wrote:
 
 On Tue, Dec 9, 2008 at 7:00 PM, BCS <ao pathlink.com> wrote:
 
 final void add(T...)(T t)
 
If all the params are the same type, typesafe variadics are a more efficient/less code-bloaty way to do it.
*And* allows overriding them!
Note the functionality that both solution provide is trivial. All the real work gets done in the _add functions that /can/ be overridden.
Dec 10 2008
parent "Denis Koroskin" <2korden gmail.com> writes:
On Wed, 10 Dec 2008 19:31:47 +0300, BCS <ao pathlink.com> wrote:

 Reply to Denis,

 On Wed, 10 Dec 2008 03:24:48 +0300, Jarrett Billingsley
 <jarrett.billingsley gmail.com> wrote:

 On Tue, Dec 9, 2008 at 7:00 PM, BCS <ao pathlink.com> wrote:

 final void add(T...)(T t)
If all the params are the same type, typesafe variadics are a more efficient/less code-bloaty way to do it.
*And* allows overriding them!
Note the functionality that both solution provide is trivial. All the real work gets done in the _add functions that /can/ be overridden.
In this case it can be made final (or avoided at all) to improve runtime performance.
Dec 10 2008