www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - some template questions

reply BCS <BCS_member pathlink.com> writes:
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
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
prev sibling parent reply Don Clugston <dac nospam.com.au> writes:
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
parent reply BCS <BCS_member pathlink.com> writes:
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.
template hang(int i) { static assert(0); const int hang = hang!(i-1); } const int x = hang!(1); OK, next question: has anyone done a sort? static assert("abc" == sort!("bca"));
May 23 2006
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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.
template hang(int i) { static assert(0); const int hang = hang!(i-1); } const int x = hang!(1); OK, next question: has anyone done a sort? static assert("abc" == sort!("bca"));
Here you go: str[1..$]); Tested it just then :) -- Daniel Keep -- v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 23 2006
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Daniel Keep wrote:
 
 [snip]




 str[1..$]);
Oops, stupid mail program. That should be 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