| Thread overview | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 
 | 
| December 09, 2008myClass.add(something)(otherthings)(thisToo); | ||||
|---|---|---|---|---|
| 
 | ||||
| Hello, How to implement an object that can do this : myClass.add(something)(otherthings)(thisToo); Is it possible ? TIA, TSalm | ||||
| December 09, 2008Re: myClass.add(something)(otherthings)(thisToo); | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to tsalm | 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;
   }
}
 | |||
| December 09, 2008Re: myClass.add(something)(otherthings)(thisToo); | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to tsalm | 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); } | |||
| December 10, 2008Re: myClass.add(something)(otherthings)(thisToo); | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to tsalm | 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);
 | |||
| December 10, 2008Re: myClass.add(something)(otherthings)(thisToo); | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to BCS | 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);
}
 | |||
| December 10, 2008Re: myClass.add(something)(otherthings)(thisToo); | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to Jarrett Billingsley | 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! | |||
| December 10, 2008Re: myClass.add(something)(otherthings)(thisToo); | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to Denis Koroskin | 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 !
 | |||
| December 10, 2008Re: myClass.add(something)(otherthings)(thisToo); | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to Denis Koroskin | 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.
 | |||
| December 10, 2008Re: myClass.add(something)(otherthings)(thisToo); | ||||
|---|---|---|---|---|
| 
 | ||||
| Posted in reply to BCS | 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.
 | |||
Copyright © 1999-2021 by the D Language Foundation
  Permalink
Permalink Reply
Reply