opDispatch(string name, E...) (E e) question.

Artur Skawina art.08.09 at gmail.com
Sun Mar 25 16:25:01 PDT 2012


On 03/26/12 00:58, bls wrote:
> Thanks Artur,
> 
> On 03/25/2012 03:18 PM, Artur Skawina wrote:
>> On 03/25/12 22:45, bls wrote:
>>> How do I "call" opDispatch(string name, E...)(E elements) ?
>>> What I want to archive is to call f.i. fm.list with an arbitrary number of arguments without using
>>>
>>> fm.list(1, "abc", 4L, 3.33);
>>>
>>> Instead I would prefer
>>> fm.list = (1, "abc", 4L, 3.33);
>>>
>>> Is this somehow possible ?
>>
>> Well, you can do
>>
>>     template ID(A...) { alias A ID; }
>>
>>     fm.list = ID!(1, "abc", 4L, 3.33);
>>
>> but is that any better than your first version above?...
> Not sure...
> 
> Maybe if we rename it
> fm.list = Values!(1,2,true);
> 
> But I does not work..  Seems I am not able to figure out the opDispatch signature.

import std.stdio;

template Values(A...) { alias A Values; }

struct FlexMap {
   template opDispatch(string key) {
      auto opDispatch(VS...)(VS vs) {
         foreach (v; vs)
            writefln("%s: (%s)%s;", key, typeof(v).stringof, v);
      }
   }
}

void main() {
   auto fm = FlexMap();
   fm.list = Values!(1, "abc", 4L, 3.33);
}

artur


More information about the Digitalmars-d-learn mailing list