www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Preliminary submission - std.rational and std.typelist

reply "Arlen" <arlen.ng gmx.com> writes:
 ----- Original Message -----
 From: David Nadlinger
 Sent: 10/06/12 01:08 PM
 To: digitalmars-d puremagic.com
 Subject: Re: Preliminary submission - std.rational and std.typelist
 
 In my experience, these cases occur a lot less frequently than 
 one might think when first encountering the problem. The solution 
 I usually go for is to just define a ConfinedTuple (or something 
 like that) template:
 
 ---
 template ConfinedTuple(T...) {
  alias T Tuple;
 }
 
 template SomeAlgorithm(alias A, alias B) {}
 ---
 
 Then, call that one algorithm using 
 SomeAlgorithm!(ConfinedTuple!A, ConfinedTuple!B). At least to me, 
 it seems to be the better tradeoff compared to re-implementing 
 all the TypeTuple functionality for another compile-time tuple 
 type.
 
 David

That does solve the initial problem, but it creates problems of its own. The initial implementation of TypeList looked like this, which is same as your ConfinedTuple: template TypeList(T...) {  alias T items; } but the problem with this is that it forces you to use 'alias' in the signature, which in turn causes code duplication later on.  You need both of these if you want, for example, fold to work with n-dimensional typelists: template Foldl(alias Fun, Z, alias TL) {  } template Foldl(alias Fun, alias Z, alias TL) { } // when Z is a TypeList Thanks, Arlen
Oct 06 2012
next sibling parent "David Nadlinger" <see klickverbot.at> writes:
On Saturday, 6 October 2012 at 20:00:47 UTC, Arlen wrote:
 You need both of these if you want, for example, fold to work 
 with n-dimensional typelists:

 template Foldl(alias Fun, Z, alias TL) {  }
 template Foldl(alias Fun, alias Z, alias TL) { } // when Z is a 
 TypeList

I'm not quite sure what you mean, as the parameter names are not quite telling. When would the first overload be needed? Actually, I'm reasonably sure that there is a solution to whatever problem you might be encountering – if maybe not obvious unless you have already learned the ins and outs of D metaprogramming the hard way. ;) By the way, could you please fix the threading behavior of your mail client? At the moment, all of your replies create a new topic on the NG web interface (forum.dlang.org), which makes the conversation somewhat hard to follow. David
Oct 06 2012
prev sibling next sibling parent Arlen <arlen.ng gmx.com> writes:
On Sat, Oct 6, 2012 at 3:53 PM, David Nadlinger <see klickverbot.at> wrote:
 On Saturday, 6 October 2012 at 20:00:47 UTC, Arlen wrote:
 You need both of these if you want, for example, fold to work with n-dim=


 template Foldl(alias Fun, Z, alias TL) {  }
 template Foldl(alias Fun, alias Z, alias TL) { } // when Z is a TypeList

I'm not quite sure what you mean, as the parameter names are not quite te=

re that there is a solution to whatever problem you might be encountering = =96 if maybe not obvious unless you have already learned the ins and outs o= f D metaprogramming the hard way. ;)

#1 template Foldl(alias Fun, Z, alias TL) { } This is called when dealing with 1-dimensional typelists. For example: alias Foldl!(MyFun, char, TL) R1; // where TL is, e.g., TypeList!(int, char, double) #2 template Foldl(alias Fun, alias Z, alias TL) { } This is called when dealing with 2-dimensions or higher. For example: alias Foldl!(MyFun, TL1, TL2) R2; // where TL1 is 1-dimensional, e.g., TypeList!(int, char), and TL2 is 2-dimensional, e.g., TypeList!(TypeList!(int, char), TypeList!(float, double)). I hope that's clear. Another problem with this is that, when the time comes to implement MyFun, chances are you will need to implement two versions of it, just like the Foldl example above. Making TypeList a struct instead of a template fixed all these problem. Maybe there another solution, but that's what I could come up with.
 By the way, could you please fix the threading behavior of your mail clie=

erface (forum.dlang.org), which makes the conversation somewhat hard to fol= low.

Sorry about that. I think it's fixed now.
 David

Oct 06 2012
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sat, Oct 6, 2012 at 11:52 PM, Arlen <arlen.ng gmx.com> wrote:

 #1
 template Foldl(alias Fun, Z, alias TL) {  }

 This is called when dealing with 1-dimensional typelists.  For example:

 alias Foldl!(MyFun, char, TL) R1;  // where TL is, e.g.,
 TypeList!(int, char, double)

 #2
 template Foldl(alias Fun, alias Z, alias TL) { }

 This is called when dealing with 2-dimensions or higher.  For example:

I had a similar project a few years ago. Here it is: https://github.com/PhilippeSigaud/dranges/blob/master/typetuple.d and, somewhat related, to show what can be done with type tuples in D (regular expressions on types) https://github.com/PhilippeSigaud/dranges/blob/master/typepattern.d
Oct 07 2012