www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Applying a tuple to a function (and more)

reply Juanjo Alvarez <juanjux gmail.com> writes:
Hi,

I've just arrived to D 2.0 and after reading Andrei's book I'm loving
everything I'm seeing (except the bugs, of course).

I wanted to ask how these would be done, because I can't find how to do it:

1. Having the strings, defined at compile time, "MyClass" and "mymethod",
how could I could I get to a delegate to MyClass.mymethod?

2. Is there any way to apply a tuple to a function, expanding as arguments?

Like:

void f(int a, double b) {}
auto tup = tuple(42, 3.14);
f(<magic goes here with tup expanding as 42, 3.14>);

Thanks,
Juanjo
Sep 18 2010
next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
--005045024396f07ad404908de9d3
Content-Type: text/plain; charset=UTF-8

On Sat, Sep 18, 2010 at 19:59, Juanjo Alvarez <juanjux gmail.com> wrote:

 I wanted to ask how these would be done, because I can't find how to do it:

 1. Having the strings, defined at compile time, "MyClass" and "mymethod",
 how could I could I get to a delegate to MyClass.mymethod?

You can insert them in a piece of code as a string, and then mix it in: enum string code = "auto deleg = &" ~ className ~ "." ~ methodName ~ ";"; // auto deleg = &MyClass.mymethod; mixin(code); // created deleg, you're good to go.
 2. Is there any way to apply a tuple to a function, expanding as arguments?

 Like:

 void f(int a, double b) {}
 auto tup = tuple(42, 3.14);
 f(<magic goes here with tup expanding as 42, 3.14>);

Use the .field or .expand alias, exposed by Tuple!() to get a direct access to the internal expression tuple. f(tup.expand); // cracks tup open Recently, bug 2800 was corrected and you can also directly access a tuple's fields by indexing: double d = tup[1]; // or simply: auto d = tup[1]; Before that, you had to do double d = tup.field[1]; Recently, tuples also got a .length member. That's handy. As for expanding tuples inside functions if you do that regularly, I suggest you use a function adaptor, like this: template tuplify(alias fun) if (isCallable!fun) { ReturnType!fun tuplify(Tuple!(ParameterTypeTuple!fun) tup) { return fun(tup.expand); } } Usage: alias tuplify!f tf; tf(tup); tf is a function, like any other. It accepts a Tuple!(int,double), cracks it open and pass it to f. I don't know if you need some explanation on how it works? Here it is anyway: tuplify is a template, a parameterized piece of code. tuplify takes only one template parameter, by alias. This means you give it a name, any identifier in the current scope. You do not give it a type. That's what allows you to write tuplify!f. Here the result of the template instantation is a function with the same name 'tuplify'. When a template exposes only one identifier with its own name, it's as if the code inside the template got instantiated right there. So tuplify!f is a function. It could have been a function template, or any other D construct. I used "alias tuplify!f tf;" to be able to use tf as ... an alias (doh!) to tuplify!f. But I could also have used: auto tf = &(tuplify!f); // tuplify!f is a function, tf is a pointer to this function. You can treat tf as any other D function : pass it around, give it as a argument to other templates or functions, etc. The template constraint if(isCallable!fun) means tuplify can only be instantiated on callables (function, delegates, pointer to functions, structs or classes with opCall defined, etc). Note that tuplify works on _all_ callables! After all, the only property I use in the code is that f can be called with (). isCallable lives here: http://digitalmars.com/d/2.0/phobos/std_traits.html#isCallable I use the ReturnType and ParameterTypeTuple templates to do a little compile-time introspection You can find ReturnType and ParameterTypeTuple here: http://digitalmars.com/d/2.0/phobos/std_traits.html#ReturnType So the internally created function accepts a Tuple!(the types fun needs) as argument. I indicate to the compiler it will return what fun returns, but I could have used auto there instead. Philippe --005045024396f07ad404908de9d3 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Sat, Sep 18, 2010 at 19:59, Juanjo Alvarez <s= pan dir=3D"ltr">&lt;<a href=3D"mailto:juanjux gmail.com">juanjux gmail.com<= /a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:= 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left= : 1ex;"> I wanted to ask how these would be done, because I can&#39;t find how to do= it:<br> <br> 1. Having the strings, defined at compile time, &quot;MyClass&quot; and &qu= ot;mymethod&quot;,<br> how could I could I get to a delegate to MyClass.mymethod?<br></blockquote>= <div><br>You can insert them in a piece of code as a string, and then mix i= t in:<br><br>enum string code =3D &quot;auto deleg =3D &amp;&quot; ~ classN= ame ~ &quot;.&quot; ~ methodName ~ &quot;;&quot;;=C2=A0=C2=A0 // auto deleg= =3D &amp;MyClass.mymethod;<br> mixin(code); // created deleg, you&#39;re good to go.<br><br>=C2=A0</div><b= lockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-= left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> <br> 2. Is there any way to apply a tuple to a function, expanding as arguments?= <br> <br> Like:<br> <br> void f(int a, double b) {}<br> auto tup =3D tuple(42, 3.14);<br> f(&lt;magic goes here with tup expanding as 42, 3.14&gt;);<br></blockquote>= <div><br><br>Use the .field or .expand alias, exposed by Tuple!() to get a = direct access to the internal expression tuple.<br><br>f(tup.expand); // cr= acks tup open<br> <br>Recently, bug 2800 was corrected and you can also directly access a tup= le&#39;s fields by indexing:<br><br>double d =3D tup[1];=C2=A0 // or simply= : =C2=A0 =C2=A0 auto d =3D tup[1];<br><br>Before that, you had to do <br><b= r>double d =3D tup.field[1];<br> <br>Recently, tuples also got a .length member. That&#39;s handy.<br>As for= expanding tuples inside functions if you do that regularly, I suggest you = use a function adaptor, like this:<br><br>template tuplify(alias fun) if (i= sCallable!fun)<br> {<br>=C2=A0=C2=A0=C2=A0 ReturnType!fun tuplify(Tuple!(ParameterTypeTuple!fu= n) tup)<br>=C2=A0=C2=A0=C2=A0 {<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 return fun(tup.expand);<br>=C2=A0=C2=A0=C2=A0 }<br>}<br><br>Usage:<br><= br>alias tuplify!f tf;<br>tf(tup);<br><br>tf is a function, like any other.= It accepts a Tuple!(int,double), cracks it open and pass it to f.<br> <br>I don&#39;t know if you need some explanation on how it works? <br>Here= it is anyway: tuplify is a template, a parameterized piece of code. tuplif= y takes only one template parameter, by alias. This means you give it a name, any identifier in the current scope. You do not give it a=20 type. That&#39;s what allows you to write tuplify!f.<br><br>Here the result= of the template instantation is a function with the same name &#39;tuplify= &#39;. When a template exposes only one identifier with its own name, it&#3= 9;s as if the code inside the template got instantiated right there.<br> So tuplify!f is a function. It could have been a function template, or any = other D construct. I used &quot;alias tuplify!f tf;&quot; to be able to use= tf as ... an alias (doh!) to tuplify!f. But I could also have used:<br> <br>auto tf =3D &amp;(tuplify!f);=C2=A0 // tuplify!f is a function, tf is a= pointer to this function.<br><br>You can treat tf as any other D function = : pass it around, give it as a argument to other templates or functions, et= c.<br> <br>The template constraint if(isCallable!fun) means tuplify can only be in= stantiated on callables (function, delegates, pointer to functions, structs= or classes with opCall defined, etc). Note that tuplify works on _all_ cal= lables! After all, the only property I use in the code is that f can be cal= led with (). <br> <br>isCallable lives here:<br><a href=3D"http://digitalmars.com/d/2.0/phobo= s/std_traits.html#isCallable">http://digitalmars.com/d/2.0/phobos/std_trait= s.html#isCallable</a><br><br>I use the ReturnType and ParameterTypeTuple te= mplates to do a little compile-time introspection<br> <br>You can find ReturnType and ParameterTypeTuple here:<br><a href=3D"http= ://digitalmars.com/d/2.0/phobos/std_traits.html#ReturnType">http://digitalm= ars.com/d/2.0/phobos/std_traits.html#ReturnType</a><br><br>So the internall= y created function accepts a Tuple!(the types fun needs) as argument. I ind= icate to the compiler it will return what fun returns, but I could have use= d auto there instead.<br> <br>Philippe<br></div></div> --005045024396f07ad404908de9d3--
Sep 18 2010
parent reply Juanjo Alvarez <juanjux gmail.com> writes:
Philippe Sigaud wrote:


 1. Having the strings, defined at compile time, "MyClass" and "mymethod",
 how could I could I get to a delegate to MyClass.mymethod?

You can insert them in a piece of code as a string, and then mix it in: enum string code = "auto deleg = &" ~ className ~ "." ~ methodName ~ ";"; // auto deleg = &MyClass.mymethod; mixin(code); // created deleg, you're good to go.

I tried it and worked like a charm (but I've changed my code so instead of two strings to class + method, a fully qualified non member function address is used so I have a little more flexibility).
 2. Is there any way to apply a tuple to a function, expanding as
 arguments?


 Use the .field or .expand alias, exposed by Tuple!() to get a direct
 access to the internal expression tuple.
 
 f(tup.expand); // cracks tup open

Now, that is exactly what I wanted. It even works if the function has aditional arguments before the expanded tuple one's: // void f(int a, int b, double c) { writeln(a); writeln(b); writeln(c); } auto tup = tuple(42, 3.14); f(1, tup.expand); // Wonderful. I wish it was better documented on http://digitalmars.com/d/2.0/phobos/std_typecons.html
 As for expanding tuples inside functions if you do that regularly, I
 suggest you use a function adaptor, like this:
 
 template tuplify(alias fun) if (isCallable!fun)
 {
     ReturnType!fun tuplify(Tuple!(ParameterTypeTuple!fun) tup)
     {
         return fun(tup.expand);
     }
 }

Nice to know. I got you excellent explanation. Thanks * 100, Juanjo
Sep 18 2010
parent reply Juanjo Alvarez <juanjux gmail.com> writes:
Philippe Sigaud wrote:

 String mixins are quite powerful, if a bit clunky at times.

Both true, for what I'm seeing of them.
 See also __traits(getMember, ...)
 http://digitalmars.com/d/2.0/traits.html   (look for getMember)

Nice, with getMembers I could redesign again my test code for using objects instead of a virtual function, but I'll keep the current design because is simpler for me (I'm still adapting my brain to all this genericity). On my current implementation with functions I still have a doubt: I'm using template-functions like this one for my callbacks: void somefunc(T...) (string regex, T args...) { foreach (arg; args) { writeln(arg); } } Then, I write the "selector" for it (in other part of the program) like this: auto t = tuple("someregex", &(module.somefunc!(int, double)),tuple(2,3.14)); And then in a third module, I get those selectors and bind them to the functions like: auto selectable = t.field[1]; selectable(t.field[0], t.field[2].expand); // Called! With your help, this works like a charm. But sometimes there will be no variable arguments to "somefunc" (only the first string), and in that case I don't know what would be the correct syntax to specify that the variable number of arguments will have no args, both for the template instantiation and for the tuple: auto t = tuple("bla", &(module.somefunc!(void), tuple(void)); // Fails auto t = tuple("bla", &(module.somefunc!(void, Tuple!(void)()); // Fails too
 Yes, typecons.Tuple has lot of nifty things going for it, but there are
 not all documented.
 You can open a bug/enhancement request at http://d.puremagic.com/issues/

I'll do it. I guess currently most of you here check the source before the documentation since the former looks to be pretty outdated sometimes.
Sep 19 2010
parent reply Juanjo Alvarez <juanjux gmail.com> writes:
Philippe Sigaud wrote:

 What languages are you used to? You seem to do quite well with genericity
 :)

What I've done profesionally and personally in the last year would be 90% Python, 5% Java and 5% C++. So yes, since Python is like a D with an uberauto and everything being runtime templates the real problem I have is with generic declarations :)
 OK, no need for the last "..."  args is already variadic. What you wrote
 is
 equivalent  "I want any number of lists of any number of types".
 Just use:
 
 void somefunc(T...)(string regex, T args) {

Changued!
 auto t = tuple("someregex", &(module.somefunc!(int,
 double)),tuple(2,3.14));

A tuple in a tuple, nice.

And maybe a tuple in a tuple in a tuple :) (see my other question later)
 auto t = tuple("bla", &(someFun), 2, 3.14);
 
 That is, the values are not wrapped in a tuple, but directly stored in t.
 That way, if you want no value, just do:
 auto t = tuple("bla", &(someFun)); // there is an expression tuple of
 length 0 there.
 
 auto selectable = t[1];
 selectable(t[0], t.expand[2..$]);

This is much better, no doubt. I've changued that part too (I'm really learning a lot with your posts, you should think about writing a book about D too). Now I've two new roadblocks: Roadblock1: If the tuple is defined in the same file as the code expanding it (with the template function in another file), it works. But if I move the tuple definition to another file (urls.d) the compiler gives me the error: Error: Unions with overlapping fields are not yet supported in CTFE urls.d(9): Error: cannot evaluate tuple("^/home/$",& index,42,3.14) at compile time urls.d(9): Error: cannot evaluate tuple("^/home/$",& index,42,3.14) at compile time That is, this works: // file: bindselector.d import std.typecons; import views; void main() { // tuple defined here auto selector_data = tuple( "^/home/$", &(views.index!(int, double)), 42, 3.14 ); auto sel_regex_var = selector_data.field[0]; auto sel_view_var = selector_data.field[1]; sel_view_var(sel_regex_var, selector_data.expand[2..$]); } But moving the declaration of "selector_data" to a file urls.d and then importing that file from bindselector.d gives me that error... bug? Roadblock2: The next step is to define multiple tuples in urls.d inside some iterable data structure, so bindselector.d can import that and do its thing inside a foreach. The problem here is related again with my limitations declaring generics. Since every tuple will have a different type signature, I tought that the logical structure to group them would be... yes, another tuple :) Then I would foreach on that structure and bind the params, etc. First try (Fail-1): // ---------------------------------------- auto selector_data_tuples = tuple( tuple( "^/home/$", &(views.index!(int, double)), 42, 3.14 ) ); foreach(selector_data; selector_data_tuples) { auto sel_regex_var = selector_data.field[0]; auto sel_view_var = selector_data.field[1]; sel_view_var(sel_regex_var, selector_data.expand[2..$]); } // ---------------------------------------- Compile error: Error: cannot infer type for selector_data Logical, since the type of the iterated element in the foreach is fixed, and I'm iterating over tuples of different types. Then I did (Fail-2): // ----------------------------------------------------------- for (int i = 0; i < selector_data_tuples.length; i++) { auto selector_data = selector_data_tuples.field[i]; auto sel_regex_var = selector_data.field[0]; auto sel_view_var = selector_data.field[1]; sel_view_var(sel_regex_var, selector_data.expand[2..$]); } // ----------------------------------------------------------- Which fails with a "Error: Integer constant expression expected instead of cast(uint)i". Is there any other way to iterate over a tuple with values of different tuples? Does d provide any other data structures better suited for this case? (other than an array of Object and a gazillion ugly casts).
Sep 19 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Juanjo Alvarez:

 But if I move the tuple 
 definition to another file (urls.d) the compiler gives me the error:

It seems a compiler bug. If not already present it probably needs to be added.
 Is there any other way to iterate over a tuple with values of different tuples?

You may use a typetuple, but those have to be used with lot of care, because the foreach on them is static, so I don't think that's what you want. D is statically typed, so you can't use it a dynamic typed language, as you are trying to do now.
 Does d provide any other data structures better suited for this 
 case? (other than an array of Object and a gazillion ugly casts). 

You may need something like an array of variants. Or to redesign your solution. Bye, bearophile
Sep 19 2010
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
Philippe Sigaud:
 Oh, but you can iterate on an expression tuple alright, even when all its
 elements have different types. I don't know why bearophile doesn't like that.

I like the iteration on typetuples (the nomenclature here is a mess, see bug 4113), but you have to use such iteration with care because it's a static foreach, your code may become very long (this is why in bug 4085 I have asked to require a "static foreach" to iterate on typetuples, to help the programmer see the difference better). Bye, bearophile
Sep 20 2010
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--00032555a3dac2e4b00490962449
Content-Type: text/plain; charset=UTF-8

On Sun, Sep 19, 2010 at 03:43, Juanjo Alvarez <juanjux gmail.com> wrote:

 enum string code = "auto deleg = &" ~ className ~ "." ~ methodName ~ ";";
 // auto deleg = &MyClass.mymethod;
 mixin(code); // created deleg, you're good to go.

I tried it and worked like a charm (but I've changed my code so instead of two strings to class + method, a fully qualified non member function address is used so I have a little more flexibility).

String mixins are quite powerful, if a bit clunky at times. See also __traits(getMember, ...) http://digitalmars.com/d/2.0/traits.html (look for getMember)
 Use the .field or .expand alias, exposed by Tuple!() to get a direct
 access to the internal expression tuple.

 f(tup.expand); // cracks tup open

Now, that is exactly what I wanted. It even works if the function has aditional arguments before the expanded tuple one's: // void f(int a, int b, double c) { writeln(a); writeln(b); writeln(c); }

Yes, because tuple.expand is directly the expression tuple stored inside Tuple!(...) When you have a typetuple in a template, like this: template List(T...) { } The T... part is a typetuple: an array of types, if you wish. You can get its length, index it, slice it, iterate on it, etc. You can have a variable with this strange 'type' and also get its length, etc. template List(T...) { T t; } t is an expression tuple. std.typecons.Tuple is nothing more than the preceding List!(...), with a few more functionalities. So, something interesting is that when you do tup.expand, you obtain a bunch of values of different types, all perfectly individually typed and usable. You can get one by indexing, slice it, etc. So, given your new f, you can even do: auto tup = tuple(42, 3.14); f(tup.expand[0], 1, tup.expand[1]); Though in this particular case, it's cleaner to do: f(tup[0], 1, tup[1]); Wonderful. I wish it was better documented on
 http://digitalmars.com/d/2.0/phobos/std_typecons.html

all documented. You can open a bug/enhancement request at http://d.puremagic.com/issues/
 Nice to know. I got you excellent explanation.

 Thanks * 100,

Philippe --00032555a3dac2e4b00490962449 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Sun, Sep 19, 2010 at 03:43, Juanjo Alvarez <s= pan dir=3D"ltr">&lt;<a href=3D"mailto:juanjux gmail.com">juanjux gmail.com<= /a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:= 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left= : 1ex;"> &gt; enum string code =3D &quot;auto deleg =3D &amp;&quot; ~ className ~ &q= uot;.&quot; ~ methodName ~ &quot;;&quot;;<br><div class=3D"im"> &gt; // auto deleg =3D &amp;MyClass.mymethod;<br> &gt; mixin(code); // created deleg, you&#39;re good to go.<br> <br> </div>I tried it and worked like a charm (but I&#39;ve changed my code so i= nstead of<br> two strings to class + method, a fully qualified non member function addres= s<br> is used so I have a little more flexibility).<br></blockquote><div><br>Stri= ng mixins are quite powerful, if a bit clunky at times.<br><br>See also __t= raits(getMember, ...)<br><a href=3D"http://digitalmars.com/d/2.0/traits.htm= l">http://digitalmars.com/d/2.0/traits.html</a>=C2=A0=C2=A0 (look for getMe= mber)<br> <br><br></div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0p= t 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><di= v class=3D"im"><br> </div><div class=3D"im">&gt; Use the .field or .expand alias, exposed by Tu= ple!() to get a direct<br> &gt; access to the internal expression tuple.<br> &gt;<br> &gt; f(tup.expand); // cracks tup open<br> <br> </div>Now, that is exactly what I wanted. It even works if the function has= <br> aditional arguments before the expanded tuple one&#39;s:<br> <br> //<br> void f(int a, int b, double c) {<br> =C2=A0 writeln(a); writeln(b); writeln(c);<br> <div class=3D"im">}<br></div></blockquote><div>=C2=A0</div><div><br>Yes, be= cause tuple.expand is directly the expression tuple stored inside Tuple!(..= .)<br>When you have a typetuple in a template, like this:<br><br>template L= ist(T...)<br> {<br>}<br><br>The T... part is a typetuple: an array of types, if you wish.= You can get its length, index it, slice it, iterate on it, etc.<br>You can= have a variable with this strange &#39;type&#39; and also get its length, = etc.<br> <br>template List(T...)<br>{<br>=C2=A0=C2=A0=C2=A0 T t;<br>}<br><br>t is an= expression tuple. std.typecons.Tuple is nothing more than the preceding Li= st!(...), with a few more functionalities.<br>So, something interesting is = that when you do tup.expand, you obtain a bunch of values of different type= s, all perfectly individually typed and usable. You can get one by indexing= , slice it, etc.<br> <br>So, given your new f, you can even do:<br><br><div class=3D"im"> auto tup =3D tuple(42, 3.14);<br> </div>f(tup.expand[0], 1, tup.expand[1]);<br><br>Though in this particular = case, it&#39;s cleaner to do:<br><br>f(tup[0], 1, tup[1]); <br><br><br></di= v><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; bor= der-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> Wonderful. I wish it was better documented on<br> <a href=3D"http://digitalmars.com/d/2.0/phobos/std_typecons.html" target=3D= "_blank">http://digitalmars.com/d/2.0/phobos/std_typecons.html</a><br> <div class=3D"im"><br></div></blockquote><div><br>Yes, typecons.Tuple has l= ot of nifty things going for it, but there are not all documented.<br>You c= an open a bug/enhancement request at <a href=3D"http://d.puremagic.com/issu= es/">http://d.puremagic.com/issues/</a><br> <br><br></div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0p= t 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><di= v class=3D"im"><br> </div>Nice to know. I got you excellent explanation.<br> <br> Thanks * 100,<br> <br></blockquote><div><br>My pleasure,<br><br>Philippe <br></div></div> --00032555a3dac2e4b00490962449--
Sep 18 2010
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--00504502cd8fc3b87f04909d1ea6
Content-Type: text/plain; charset=UTF-8

 instead of a virtual function, but I'll keep the current design because is
 simpler for me (I'm still adapting my brain to all this genericity).

What languages are you used to? You seem to do quite well with genericity :)
 On my current implementation with functions I still have a doubt:

 I'm using template-functions like this one for my callbacks:

 void somefunc(T...) (string regex, T args...) {
    foreach (arg; args) {
        writeln(arg);
    }
 }

OK, no need for the last "..." args is already variadic. What you wrote is equivalent "I want any number of lists of any number of types". Just use: void somefunc(T...)(string regex, T args) { ...
 Then, I write the "selector" for it (in other part of the program) like
 this:

 auto t = tuple("someregex", &(module.somefunc!(int,
 double)),tuple(2,3.14));

A tuple in a tuple, nice.
 And then in a third module, I get those selectors and bind them to the
 functions like:

 auto selectable = t.field[1];
 selectable(t.field[0], t.field[2].expand); // Called!

 With your help, this works like a charm.

 But sometimes there will be no variable arguments to "somefunc" (only the
 first string), and in that case I don't know what would be the correct
 syntax to specify that the variable number of arguments will have no args,
 both for the template instantiation and for the tuple:

 auto t = tuple("bla", &(module.somefunc!(void), tuple(void)); // Fails
 auto t = tuple("bla", &(module.somefunc!(void, Tuple!(void)()); // Fails
 too

Use tuple(). Or Tuple!()() Another possibility could be to write your selector like this: auto t = tuple("bla", &(someFun), 2, 3.14); That is, the values are not wrapped in a tuple, but directly stored in t. That way, if you want no value, just do: auto t = tuple("bla", &(someFun)); // there is an expression tuple of length 0 there. auto selectable = t[1]; selectable(t[0], t.expand[2..$]); for now, you cannot directly get a slice from the tuple. You either use expand (an expression tuple, so slicable) or use the Tuple.slice member function (never used it). As to the difference between Tuple!(void) and Tuple!(): void is a 'valid' type (sort of). It's just a type that has no value. So Tuple!(void) and Tuple!() are different. You can define the type Tuple!(int, void, int), but you cannot create a value of that type, because the void field cannot have a value. Tuple!() is a tuple with no field, an empty tuple. You can create it all right. It's just, its length is zero, and it has no field. If you open it with expand, you get a zero-length typetuple. It's like an array of length zero: it's valid, but you cannot access its internal values, because there is none. Other programming languages call this the Unit type, because it's a (the) type that has only _one_ value, namely Tuple!()(). If you ask for the parameter typetuple for a no-arg function, you'll get a zero-length typetuple: int foo() { return 0;} alias ParameterTypeTuple!foo Pfoo; assert(is(Pfoo == TypeTuple!()); Pfoo pfoo; pfoo is a tuple of length 0. auto a = foo(pfoo); // we can pass it to a function with no arg. Philippe --00504502cd8fc3b87f04909d1ea6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote"><br><blockquote class=3D"gmail_quote" style=3D"m= argin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); paddin= g-left: 1ex;"> instead of a virtual function, but I&#39;ll keep the current design because= is<br> simpler for me (I&#39;m still adapting my brain to all this genericity).<br=
</blockquote><div><br>What languages are you used to? You seem to do quite=

tyle=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204= ); padding-left: 1ex;"> <br> On my current implementation with functions I still have a doubt:<br> <br> I&#39;m using template-functions like this one for my callbacks:<br> <br> void somefunc(T...) (string regex, T args...) {<br> =C2=A0 =C2=A0foreach (arg; args) {<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0writeln(arg);<br> =C2=A0 =C2=A0}<br> }<br></blockquote><div><br>OK, no need for the last &quot;...&quot;=C2=A0 a= rgs is already variadic. What you wrote is equivalent=C2=A0 &quot;I want an= y number of lists of any number of types&quot;. <br>Just use:<br><br>void s= omefunc(T...)(string regex, T args) {<br> ...<br><br>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0= pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: = 1ex;"> <br> Then, I write the &quot;selector&quot; for it (in other part of the program= ) like<br> this:<br> <br> auto t =3D tuple(&quot;someregex&quot;, &amp;(module.somefunc!(int, double)= ),tuple(2,3.14));<br></blockquote><div><br>A tuple in a tuple, nice.<br>=C2= =A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8= ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> <br> And then in a third module, I get those selectors and bind them to the<br> functions like:<br> <br> auto selectable =3D t.field[1];<br> selectable(t.field[0], t.field[2].expand); // Called!<br> <br> With your help, this works like a charm.<br> <br> But sometimes there will be no variable arguments to &quot;somefunc&quot; (= only the<br> first string), and in that case I don&#39;t know what would be the correct<= br> syntax to specify that the variable number of arguments will have no args,<= br> both for the template instantiation and for the tuple:<br> <br> auto t =3D tuple(&quot;bla&quot;, &amp;(module.somefunc!(void), tuple(void)= ); // Fails<br> auto t =3D tuple(&quot;bla&quot;, &amp;(module.somefunc!(void, Tuple!(void)= ()); // Fails too<br></blockquote><div><br>Use tuple(). Or Tuple!()()<br><b= r>Another possibility could be to write your selector like this:<br><br> auto t =3D tuple(&quot;bla&quot;, &amp;(someFun), 2, 3.14);<br><br>That is,= the values are not wrapped in a tuple, but directly stored in t.<br>That w= ay, if you want no value, just do:<br>auto t =3D tuple(&quot;bla&quot;, &am= p;(someFun)); // there is an expression tuple of length 0 there.<br> <br>auto selectable =3D t[1];<br>selectable(t[0], t.expand[2..$]);<br><br>f= or now, you cannot directly get a slice from the tuple. You either use expa= nd (an expression tuple, so slicable) or use the Tuple.slice member functio= n (never used it).<br> <br><br>As to the difference between Tuple!(void) and Tuple!():<br>void is = a &#39;valid&#39; type (sort of). It&#39;s just a type that has no value. S= o Tuple!(void) and Tuple!() are different.<br>You can define the type Tuple= !(int, void, int), but you cannot create a value of that type, because the = void field cannot have a value.<br> <br>Tuple!() is a tuple with no field, an empty tuple. You can create it al= l right. It&#39;s just, its length is zero, and it has no field. <br>If you= open it with expand, you get a zero-length typetuple. It&#39;s like an arr= ay of length zero: it&#39;s valid, but you cannot access its internal value= s, because there is none. Other programming languages call this the Unit ty= pe, because it&#39;s a (the) type that has only _one_ value, namely Tuple!(= )().<br> <br><br>If you ask for the parameter typetuple for a no-arg function, you&#= 39;ll get a zero-length typetuple:<br><br>int foo() { return 0;}<br>alias P= arameterTypeTuple!foo Pfoo;<br>assert(is(Pfoo =3D=3D TypeTuple!());<br><br> Pfoo pfoo; pfoo is a tuple of length 0.<br>auto a =3D foo(pfoo); // we can = pass it to a function with no arg.<br><br><br>Philippe<br></div></div> --00504502cd8fc3b87f04909d1ea6--
Sep 19 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--001636c5a4105f1d7a0490b618f6
Content-Type: text/plain; charset=UTF-8

On Sun, Sep 19, 2010 at 18:33, Juanjo Alvarez <juanjux gmail.com> wrote:

 Philippe Sigaud wrote:

 What languages are you used to? You seem to do quite well with genericity
 :)

What I've done profesionally and personally in the last year would be 90% Python, 5% Java and 5% C++. So yes, since Python is like a D with an uberauto and everything being runtime templates the real problem I have is with generic declarations :)

OK, that's why you're using tuples for everything :) Python's tuples are cool. I think I *learnt* what tuples were while dabbling in Python a few years ago. (Haskell's ones are great too). Note that the 'real' D tuples are expression tuples (instantiated typetuples). The "T..." thingies in templates, once instantiated with T t; They know their length, are indexable, slicable, iterable, etc. In a way they are random-access range with a length, only with heterogeneous types. But I don't think they were conceived as tuples in other languages, not at first. I'd guess that, for Walter, they were just a natural extension/cleaning for templates, based on C++. But they are like Python's tuples! Except for one blatant limitation: they cannot be returned from a function :( But you can return a templated struct. So the basic idea is to wrap them in Tuple(T...) to be able to return them. Then std.typecons.tuple does its best to offer a interesting access to the underlying expression tuple. But it's not a first-class citizen in D: you cannot iterate them nor slice them... For now, because the situation has improved. What many people coming from C++/Java do is putting their stuff in structs/classes. They tend not to use tuples that much. This is much better, no doubt. I've changued that part too (I'm really
 learning a lot with your posts, you should think about writing a book about
 D too).

What I should do is moving my a** and have some code reviewed for Phobos. But it seems I can't find the time. When I've half an hour, I try to help here instead :)
 Now I've two new roadblocks:

 Roadblock1:

 If the tuple is defined in the same file as the code expanding it (with the
 template function in another file), it works. But if I move the tuple
 definition to another file (urls.d) the compiler gives me the error:

 Error: Unions with overlapping fields are not yet supported in CTFE
 urls.d(9): Error: cannot evaluate tuple("^/home/$",& index,42,3.14) at
 compile time
 urls.d(9): Error: cannot evaluate tuple("^/home/$",& index,42,3.14) at
 compile time

 That is, this works:

 // file: bindselector.d
 import std.typecons;
 import views;

 void main() {
     // tuple defined here
     auto selector_data = tuple( "^/home/$", &(views.index!(int, double)),
 42, 3.14 );
     auto sel_regex_var  = selector_data.field[0];
     auto sel_view_var = selector_data.field[1];
     sel_view_var(sel_regex_var, selector_data.expand[2..$]);
 }


 But moving the declaration of "selector_data" to a file urls.d and then
 importing that file from bindselector.d gives me that error... bug?

What do you mean, you moved the declaration of selector_data ? You declared it outside main() in another module? I don't know why, I never declare date outside functions/objects. Maybe if you declare it in the 'root scope' of the module (I don't know how to call that: the basic scope of a module, the one where the module name; is), it's initialized at compile time. And at compile time, I'm not sure things like addresses &(...) have a sense. Did you try to make selector_data a function? auto selector_data() { return tuple("^/home/$", &(views.index!(int, double)),42, 3.14 );}
 Roadblock2:
 The next step is to define multiple tuples in urls.d inside some iterable
 data structure, so bindselector.d can import that and do its thing inside a
 foreach.

 The problem here is related again with my limitations declaring generics.
 Since every tuple will have a different type signature, I tought that the
 logical structure to group them would be... yes, another tuple :) Then I
 would foreach on that structure and bind the params, etc.

 First try (Fail-1):

 // ----------------------------------------
 auto selector_data_tuples = tuple(
                                   tuple( "^/home/$", &(views.index!(int,
 double)), 42, 3.14 )
                                 );

 foreach(selector_data; selector_data_tuples) {
     auto sel_regex_var  = selector_data.field[0];
     auto sel_view_var = selector_data.field[1];
     sel_view_var(sel_regex_var, selector_data.expand[2..$]);
 }
 // ----------------------------------------

 Compile error: Error: cannot infer type for selector_data

 Logical, since the type of the iterated element in the foreach is fixed,
 and
 I'm iterating over tuples of different types.

Oh, but you can iterate on an expression tuple alright, even when all its elements have different types. I don't know why bearophile doesn't like that. It's just, as I've said higher up in this message, that tuple()/Tuple!() is a struct, not a 'real' tuple: it offers a nice access to the wrapped expression tuple, but not as much as you'd like. Use .expand to get direct access: auto t = tuple(1, 3.14, "abc"); foreach(i, field; t.expand) // get index and value { writefln("field #%s is of type %s and has value %s.",i,typeof(field).stringof, to!string(field)); } If you want to do some type sorcery, you can also iterate on the types. You can get access to them by the .Types alias in Tuple!(). foreach(i,Type; t.Types) // will iterate on (int,double,string) and not on (1, 3.14, "abc") {...} Philippe --001636c5a4105f1d7a0490b618f6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sun, Sep 19, 2010 at 18:33, Juanjo Al= varez <span dir=3D"ltr">&lt;<a href=3D"mailto:juanjux gmail.com">juanjux gm= ail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D= "margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padd= ing-left: 1ex;"> <div class=3D"im">Philippe Sigaud wrote:<br> <br> &gt; What languages are you used to? You seem to do quite well with generic= ity<br> &gt; :)<br> <br> </div>What I&#39;ve done profesionally and personally in the last year woul= d be 90%<br> Python, 5% Java and 5% C++. So yes, since Python is like a D with an<br> uberauto and everything being runtime templates the real problem I have is<= br> with generic declarations :)<br></blockquote><div><br>OK, that&#39;s why yo= u&#39;re using tuples for everything :)<br>Python&#39;s tuples are cool. I = think I *learnt* what tuples were while dabbling in Python a few years ago.= (Haskell&#39;s ones are great too).<br> <br>Note that the &#39;real&#39; D tuples are expression tuples (instantiat= ed typetuples). The &quot;T...&quot; thingies in templates, once instantiat= ed with=C2=A0 T t;<br>They know their length, are indexable, slicable, iter= able, etc. In a way they are random-access range with a length, only with h= eterogeneous types. <br> But I don&#39;t think they were conceived as tuples in other languages, not= at first. I&#39;d guess that, for Walter, they were just a natural extensi= on/cleaning for templates, based on C++. But they are like Python&#39;s tup= les! Except for one blatant limitation: they cannot be returned from a func= tion :(<br> But you can return a templated struct. So the basic idea is to wrap them in= Tuple(T...) to be able to return them. Then std.typecons.tuple does its be= st to offer a interesting access to the underlying expression tuple. But it= &#39;s not a first-class citizen in D: you cannot iterate them nor slice th= em... For now, because the situation has improved.<br> <br>What many people coming from C++/Java do is putting their stuff in stru= cts/classes. They tend not to use tuples that much.<br><br><br><br></div><b= lockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-= left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> <div class=3D"im"> </div>This is much better, no doubt. I&#39;ve changued that part too (I&#39= ;m really<br> learning a lot with your posts, you should think about writing a book about= <br> D too).<br></blockquote><div><br>What I should do is moving my a** and have= some code reviewed for Phobos. But it seems I can&#39;t find the time. Whe= n I&#39;ve half an hour, I try to help here instead :)<br><br><br>=C2=A0</d= iv> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> Now I&#39;ve two new roadblocks:<br> <br> Roadblock1:<br> <br> If the tuple is defined in the same file as the code expanding it (with the= <br> template function in another file), it works. But if I move the tuple<br> definition to another file (urls.d) the compiler gives me the error:<br> <br> Error: Unions with overlapping fields are not yet supported in CTFE<br> urls.d(9): Error: cannot evaluate tuple(&quot;^/home/$&quot;,&amp; index,42= ,3.14) at<br> compile time<br> urls.d(9): Error: cannot evaluate tuple(&quot;^/home/$&quot;,&amp; index,42= ,3.14) at<br> compile time<br> <br> That is, this works:<br> <br> // file: bindselector.d<br> import std.typecons;<br> import views;<br> <br> void main() {<br> =C2=A0 =C2=A0 // tuple defined here<br> =C2=A0 =C2=A0 auto selector_data =3D tuple( &quot;^/home/$&quot;, &amp;(vi= ews.index!(int, double)),<br> 42, 3.14 );<br> =C2=A0 =C2=A0 auto sel_regex_var =C2=A0=3D selector_data.field[0];<br> =C2=A0 =C2=A0 auto sel_view_var =3D selector_data.field[1];<br> =C2=A0 =C2=A0 sel_view_var(sel_regex_var, selector_data.expand[2..$]);<br> }<br> <br> <br> But moving the declaration of &quot;selector_data&quot; to a file urls.d an= d then<br> importing that file from bindselector.d gives me that error... bug?<br></bl= ockquote><div><br>What do you mean, you moved the declaration of selector_d= ata ? You declared it outside main() in another module? I don&#39;t know wh= y, I never declare date outside functions/objects. <br> <br>Maybe if you declare it in the &#39;root scope&#39; of the module (I do= n&#39;t know how to call that: the basic scope of a module, the one where t= he module name; is), it&#39;s initialized at compile time. And at compile t= ime, I&#39;m not sure things like addresses &amp;(...) have a sense.<br> <br>Did you try to make selector_data a function?<br> <br> auto selector_data() { return tuple(&quot;^/home/$&quot;, &amp;(views.index= !(int, double)),42, 3.14 );}<br><br>=C2=A0</div><blockquote class=3D"gmail_= quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, = 204, 204); padding-left: 1ex;"> Roadblock2:<br> The next step is to define multiple tuples in urls.d inside some iterable<b= r> data structure, so bindselector.d can import that and do its thing inside a= <br> foreach.<br> <br> The problem here is related again with my limitations declaring generics.<b= r> Since every tuple will have a different type signature, I tought that the<b= r> logical structure to group them would be... yes, another tuple :) Then I<br=

<br> First try (Fail-1):<br> <br> // ----------------------------------------<br> auto selector_data_tuples =3D tuple(<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 tuple( &quot;^/home/$&quot;, = &amp;(views.index!(int,<br> double)), 42, 3.14 )<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 );<br> <br> foreach(selector_data; selector_data_tuples) {<br> =C2=A0 =C2=A0 auto sel_regex_var =C2=A0=3D selector_data.field[0];<br> =C2=A0 =C2=A0 auto sel_view_var =3D selector_data.field[1];<br> =C2=A0 =C2=A0 sel_view_var(sel_regex_var, selector_data.expand[2..$]);<br> }<br> // ----------------------------------------<br> <br> Compile error: Error: cannot infer type for selector_data<br> <br> Logical, since the type of the iterated element in the foreach is fixed, an= d<br> I&#39;m iterating over tuples of different types.<br></blockquote><div><br>= Oh, but you can iterate on an expression tuple alright, even when all its e= lements have different types. I don&#39;t know why bearophile doesn&#39;t l= ike that.<br> It&#39;s just, as I&#39;ve said higher up in this message, that tuple()/Tup= le!() is a struct, not a &#39;real&#39; tuple: it offers a nice access to t= he wrapped expression tuple, but not as much as you&#39;d like. Use .expand= to get direct access:<br> <br><br>=C2=A0=C2=A0=C2=A0 auto t =3D tuple(1, 3.14, &quot;abc&quot;);<br>= =C2=A0=C2=A0=C2=A0 foreach(i, field; t.expand) // get index and value<br>= =C2=A0=C2=A0=C2=A0 {<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 writefln= (&quot;field #%s is of type %s and has value %s.&quot;,i,typeof(field).stri= ngof, to!string(field));<br> =C2=A0=C2=A0=C2=A0 }<br><br>If you want to do some type sorcery, you can al= so iterate on the types. You can get access to them by the .Types alias in = Tuple!(). <br><br>foreach(i,Type;=C2=A0 t.Types) // will iterate on (int,do= uble,string) and not on (1, 3.14, &quot;abc&quot;)<br> {...}<br><br><br>Philippe<br><br></div></div> --001636c5a4105f1d7a0490b618f6--
Sep 20 2010