Debugging CTFE
Matthias Walter
Matthias.Walter at st.ovgu.de
Thu Jun 19 03:07:51 PDT 2008
Koroskin Denis Wrote:
> On Tue, 17 Jun 2008 20:56:00 +0400, BCS <ao at pathlink.com> wrote:
>
> > Reply to Koroskin,
> >
> >> On Tue, 17 Jun 2008 20:04:25 +0400, Matthias Walter
> >> <Matthias.Walter at st.ovgu.de> wrote:
> >>
> >>> Hello,
> >>> I have written some compile time executable functions with D 1.0
> >>> which work in runtime but hang (and allocate memory without end) at
> >>> compile-time. Is there a way to debug this further? Can one print
> >>> stuff out? (Don't know if writefln works at compile-time, as I'm
> >>> using Tango) Can I somehow get a stack trace of the functions
> >>> called?
> >>> best regards
> >>> Matthias Walter
> >> You can use pragma(msg, "hello there!") to output some text at compile
> >> time.
> >>
> >
> > I don't /think/ that works for what is wanted:
> >
> > char[] CTFE()
> > {
> > char[] c = "hello world", for(;c.length >0; c=c[0..$-1])
> > pragma(msg, c);
> >
> > return c;
> > }
> >
> >
> > // not tested
> >
> >
>
> You are right. But although it doesn't work (currently) for CTFE, it does
> for templates:
>
> template itoa(uint i)
> {
> static if (i < 10)
> const char[] itoa = "" ~ cast(char)(i + '0');
> else
> const char[] itoa = itoa!(i / 10) ~ itoa!(i % 10);
> }
>
> template factorial(int i)
> {
> pragma(msg, "Calculating factorial of " ~ itoa!(i));
>
> static if (i < 2) {
> const int factorial = 1;
> } else {
> const int factorial = factorial!(i-1)*i;
> }
> }
>
> int main()
> {
> const int t = factorial!(100);
> return 0;
> }
Yes, the problem is, that compile-time functions must also be runtime-executable (and pragma must not depend on runtime variables), which I think, should not be a necessarity. But I guess this makes sense from Walters point of view, so this won't change in near future, I guess.
More information about the Digitalmars-d-learn
mailing list