digitalmars.D.learn - some template questions
- BCS <BCS_member pathlink.com> May 20 2006
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> May 20 2006
- Don Clugston <dac nospam.com.au> May 22 2006
- BCS <BCS_member pathlink.com> May 23 2006
- Daniel Keep <daniel.keep.lists gmail.com> May 23 2006
- Daniel Keep <daniel.keep.lists gmail.com> May 23 2006
Two questions:
This is supposed to make a list of all numbers from 0 to i, can it be made to
work, and if so how?
template list(int i)
{
static if(i==0)
int[] list = [0];
else
int[] list = list!(i-1) ~ i;
}
Should this hang or should the static assert kill it before it loops? (as of
0.157, it hangs)
template hang()
{
static assert(false);
const int hang = hang!();
}
May 20 2006
"BCS" <BCS_member pathlink.com> wrote in message news:e4oca5$8pj$1 digitaldaemon.com...This is supposed to make a list of all numbers from 0 to i, can it be made to work, and if so how? template list(int i) { static if(i==0) int[] list = [0]; else int[] list = list!(i-1) ~ i; }
This seems to be a problem in D. I say this because Don Clugston, who knows more about templates than anyone I've ever met, does the following: http://svn.dsource.org/projects/ddl/trunk/meta/generatetable.d http://svn.dsource.org/projects/ddl/trunk/meta/hack/hackgenerate.d He just uses a templated "generator" function instead of a fixed function to create the array, but the point is that you'll notice he uses that hack, which generates the array by brute force - by just creating the elements statically!
May 20 2006
BCS wrote:Two questions: This is supposed to make a list of all numbers from 0 to i, can it be made to work, and if so how?
It doesn't work because D doesn't have array literals yet. (But since D *does* have char[] and dchar[] literals, you can do some nasty casts to get the effect you want).template list(int i) { static if(i==0) int[] list = [0]; else int[] list = list!(i-1) ~ i; } Should this hang or should the static assert kill it before it loops? (as of 0.157, it hangs) template hang() { static assert(false); const int hang = hang!(); }
The file below doesn't hang for me in DMD 0.156 Windows, it gives a sensible error message: ----- template hang() { static assert(0); const int hang = hang!(); } const int x = hang!(); ----- If that it fails for you, enter it in Bugzilla as a regression or Linux-only bug.
May 22 2006
In article <e4ub7s$2v7l$1 digitaldaemon.com>, Don Clugston says...BCS wrote:Two questions:
It doesn't work because D doesn't have array literals yet.
??? this works (I was using it tonight): struct fo {int i} fo[] bar = [ {i:5}, {i:6} ]; --------------------------template hang() { static assert(false); const int hang = hang!(); }
----- If that it fails for you, enter it in Bugzilla as a regression or Linux-only bug.
#$% faulty memory (the gray wet stuff, not the silicon suff). Try: template hang(int i) { static assert(0); const int hang = hang!(i-1); } const int x = hang!(1); this hangs 0.157 on Linux and XP (now bug #152) OK, next question: has anyone done a sort? static assert("abc" == sort!("bca"));
May 23 2006
BCS wrote:In article <e4ub7s$2v7l$1 digitaldaemon.com>, Don Clugston says...BCS wrote:Two questions:
It doesn't work because D doesn't have array literals yet.
??? this works (I was using it tonight): struct fo {int i} fo[] bar = [ {i:5}, {i:6} ]; --------------------------template hang() { static assert(false); const int hang = hang!(); }
----- If that it fails for you, enter it in Bugzilla as a regression or Linux-only bug.
#$% faulty memory (the gray wet stuff, not the silicon suff). Try: template hang(int i) { static assert(0); const int hang = hang!(i-1); } const int x = hang!(1); this hangs 0.157 on Linux and XP (now bug #152) OK, next question: has anyone done a sort? static assert("abc" == sort!("bca"));
Here you go: # # template insertChar(char[] chr, char[] str) # { # static if( str.length == 0 ) # const char[] insertChar = chr; # else static if( chr[0] <= str[0] ) # const char[] insertChar = chr ~ str; # else # const char[] insertChar = str[0..1] ~ insertChar!(chr, # str[1..$]); # } # # template sort(char[] str) # { # static if( str.length == 0 ) # const char[] sort = ""; # else # const char[] sort = insertChar!(str[0..1], sort!(str[1..$])); # } # # template slice(char[] str) # { # const char[] slice = str[1..$-1]; # } # # pragma(msg, `sort!("bsakdjfyiu4r") == ` ~ sort!("bsakdjfyiu4r")); # # void # main() # { # } # Tested it just then :) -- Daniel Keep -- v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 23 2006
Daniel Keep wrote:[snip] # else static if( chr[0] <= str[0] ) # const char[] insertChar = chr ~ str; # else # const char[] insertChar = str[0..1] ~ insertChar!(chr, # str[1..$]);
Oops, stupid mail program. That should be # const char[] insertChar = str[0..1] ~ insertChar!(chr, str[1..$]); Also, you could easily generalise this by changing 'char' to whatever type you want to use. That is left as an exercise for the reader :P -- Daniel -- v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 23 2006









"Jarrett Billingsley" <kb3ctd2 yahoo.com> 