digitalmars.D.learn - myClass.add(something)(otherthings)(thisToo);
- tsalm (6/6) Dec 09 2008 Hello,
- Ellery Newcomer (10/19) Dec 09 2008 Something like this might work:
- Denis Koroskin (53/59) Dec 09 2008 Yes:
- BCS (12/20) Dec 09 2008 if you don't mind dropping the )(
- Jarrett Billingsley (8/18) Dec 09 2008 If all the params are the same type, typesafe variadics are a more
- Denis Koroskin (2/22) Dec 09 2008 *And* allows overriding them!
- tsalm (3/28) Dec 09 2008 Thanks you all !
- BCS (3/15) Dec 10 2008 Note the functionality that both solution provide is trivial. All the re...
- Denis Koroskin (3/18) Dec 10 2008 In this case it can be made final (or avoided at all) to improve runtime...
Hello, How to implement an object that can do this : myClass.add(something)(otherthings)(thisToo); Is it possible ? TIA, TSalm
Dec 09 2008
tsalm wrote:Hello, How to implement an object that can do this : myClass.add(something)(otherthings)(thisToo); Is it possible ? TIA, TSalmSomething like this might work: class MyClass{ int[] stuff; alias add opCall; MyClass add(int k){ stuff ~= k; return this; } }
Dec 09 2008
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, TSalmYes: 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
Reply to TSalm,Hello, How to implement an object that can do this : myClass.add(something)(otherthings)(thisToo); Is it possible ? TIA, TSalmif 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
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
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:*And* allows overriding them!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
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:Thanks you all !On Tue, Dec 9, 2008 at 7:00 PM, BCS <ao pathlink.com> wrote:*And* allows overriding them!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
Reply to Denis,On Wed, 10 Dec 2008 03:24:48 +0300, Jarrett Billingsley <jarrett.billingsley gmail.com> wrote:Note the functionality that both solution provide is trivial. All the real work gets done in the _add functions that /can/ be overridden.On Tue, Dec 9, 2008 at 7:00 PM, BCS <ao pathlink.com> wrote:*And* allows overriding them!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.
Dec 10 2008
On Wed, 10 Dec 2008 19:31:47 +0300, BCS <ao pathlink.com> wrote:Reply to Denis,In this case it can be made final (or avoided at all) to improve runtime performance.On Wed, 10 Dec 2008 03:24:48 +0300, Jarrett Billingsley <jarrett.billingsley gmail.com> wrote:Note the functionality that both solution provide is trivial. All the real work gets done in the _add functions that /can/ be overridden.On Tue, Dec 9, 2008 at 7:00 PM, BCS <ao pathlink.com> wrote:*And* allows overriding them!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.
Dec 10 2008