www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opDispatch(string name, E...) (E e) question.

reply bls <bizprac orange.fr> writes:
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 ?

import std.variant;
import std.conv;
....
auto fm = FlexMap();

fm.ten = 10;
fm.ten = ["Ten", "Zehn", "Tein"];
fm.list = [20, 10, 2, 2, 44 ] ;
fm.list = "Hello opDispatch";

struct FlexMap
{
     Variant[] [string] map;

     Variant[] opDispatch(string name)()
     {
        return map[name];
     }
	
     Variant[] opDispatch(string name, E...)(E elements)
     {
	foreach(element; elements)
        		map[name] ~= to!Variant(element);
        	return map[name];
     }
	
     Variant[] opDispatch(string name, T) (T t)
     {
	map[name] ~= to!Variant(t);
	return map[name];
     }		
}



Another question :
How do I bring in :

opDispatch(string name, T) (T[] t)

into FlexMap ?

TIA, Bjoern
Mar 25 2012
next sibling parent reply James Miller <james aatch.net> writes:
On 26 March 2012 09:45, bls <bizprac orange.fr> 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);
You can use property on opDispatch to use setter/getter notation, however I wouldn't rely on that functionality long-term if you want to keep the same function-call syntax (since -property flag is supposed to enforce proper parenthesis use on ` property`s).
 fm.list = (1, "abc", 4L, 3.33);
I'm hoping you mean `fm.list = [1, "abc", 4L, 3.33];` I think that using the right template parameters, you can use the same code for (T...)(T el) and (T)(T[]), I just can't remember what that is...
 Another question :
 How do I bring in :

 opDispatch(string name, T) (T[] t)
-- James Miller
Mar 25 2012
parent reply bls <bizprac orange.fr> writes:
On 03/25/2012 02:04 PM, James Miller wrote:
 On 26 March 2012 09:45, bls<bizprac orange.fr>  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);
You can use property on opDispatch to use setter/getter notation, however I wouldn't rely on that functionality long-term if you want to keep the same function-call syntax (since -property flag is supposed to enforce proper parenthesis use on ` property`s).
 fm.list = (1, "abc", 4L, 3.33);
I'm hoping you mean `fm.list = [1, "abc", 4L, 3.33];` I think that using the right template parameters, you can use the same code for (T...)(T el) and (T)(T[]), I just can't remember what that is...
Ouch, yep, I mean [1, "abc", 4L, 3.33] But I have no clue how to implement it.
 Another question :
 How do I bring in :

 opDispatch(string name, T) (T[] t)
-- James Miller
(T) (T[] t) AND (T) (T t) seems not to work. snip struct FlexMap { Variant[] [string] map; Variant[] opDispatch(string name)() { return map[name]; } Variant[] opDispatch(string name, E...)(E elements) { foreach(element; elements) map[name] ~= to!Variant(element); return properties[name]; } Variant[] opDispatch(string name, T) (T t) { map[name] ~= to!Variant(t); return map[name]; } // No go Variant[] opDispatch(string name, T) (T[] t) {} }
Mar 25 2012
parent reply James Miller <james aatch.net> writes:
On 26 March 2012 10:34, bls <bizprac orange.fr> wrote:
 (T) (T[] t) AND (T) (T t) seems not to work.
Ok, so looking here: http://dlang.org/function.html, I have determined that, if you are using Variant arrays (though I'm not sure if you can do that using literals...) you can use the syntax from this example: int test() { return sum(1, 2, 3) + sum(); // returns 6+0 } int func() { int[3] ii = [4, 5, 6]; return sum(ii); // returns 15 } int sum(int[] ar ...) { int s; foreach (int x; ar) s += x; return s; } You'll probably need to do some experimentation to figure out how Variant fits into that properly, but it shouldn't be too hard. Also, remember that opDispatch takes the name of the function as the last parameter, so watch out for that. -- James Miller
Mar 25 2012
parent bls <bizprac orange.fr> writes:
On 03/25/2012 02:59 PM, James Miller wrote:
 Ok, so looking here:http://dlang.org/function.html, I have determined
 that, if you are using Variant arrays (though I'm not sure if you can
 do that using literals...) you can use the syntax from this example:
Thanks James.. will give it tomorrow a new try. At least Variant[] va = [1, 2.3222, "abc"]; is not working. Guess I have to give up the opDispatch() thing and create it a bit more traditional :) Bjoern oh, beside you mean : opDispatch(string name, T) (T[] t...)
Mar 25 2012
prev sibling next sibling parent reply Artur Skawina <art.08.09 gmail.com> writes:
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?... artur
Mar 25 2012
parent reply bls <bizprac orange.fr> writes:
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. Think I will rewrite it without using opDispatch.
 artur
Mar 25 2012
parent Artur Skawina <art.08.09 gmail.com> writes:
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
Mar 25 2012
prev sibling parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Sun, 25 Mar 2012 22:45:57 +0200, bls <bizprac orange.fr> 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 ?
No. However, this is: fm.list = tuple( 1, "abc", 4L, 3.33 ); In that case, you probably want to use the overload opDispatch(string name, E...)(Tuple!E elements) and mark the other: opDispatch(string name, E...)(E element) if ( !(E.length == 1 && isTuple!(E[0]))) tuple, Tuple, and isTuple are found in std.typecons.
 import std.variant;
 import std.conv;
 ....
 auto fm = FlexMap();

 fm.ten = 10;
 fm.ten = ["Ten", "Zehn", "Tein"];
 fm.list = [20, 10, 2, 2, 44 ] ;
 fm.list = "Hello opDispatch";

 struct FlexMap
 {
      Variant[] [string] map;

      Variant[] opDispatch(string name)()
      {
         return map[name];
      }
 	
      Variant[] opDispatch(string name, E...)(E elements)
      {
 	foreach(element; elements)
         		map[name] ~= to!Variant(element);
         	return map[name];
      }
 	
      Variant[] opDispatch(string name, T) (T t)
      {
 	map[name] ~= to!Variant(t);
 	return map[name];
      }		
 }



 Another question :
 How do I bring in :

 opDispatch(string name, T) (T[] t)

 into FlexMap ?
It's possible to do with template constraints: import std.traits : isArray; void opDispatch(string name, T)(T t) if ( isArray!T ) { // Array specific. } void opDispatch(string name, T)(T t) if ( !isArray!T) { // Single element. } One could also use static if: Variant[] opDispatch(string name, T)(T t) { static if ( is( T U : U[] ) ) { map[name] ~= to! } else { // Whatever you want to do differently here. } }
Mar 25 2012