www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - foreach on a tuple in CTF

reply Max Samukha <samukha voliacable.com> writes:
Why the function cannot be evaluated at compile time? Is it a bug? 

import std.stdio;

char[] foo(A...)()
{
    char[] s = "";

    foreach (i, v; A)
    {
        static if (i == 1)
            s = i.stringof;
    }

    return s;
}

void main(char[][] args)
{
    alias foo!("test", 100) f;

    writefln(f());		// works				
    pragma(msg, f());	// error: cannot evaluate at compile time
}
Apr 24 2007
parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Max Samukha wrote:
 Why the function cannot be evaluated at compile time? Is it a bug? 
 
 import std.stdio;
 
 char[] foo(A...)()
 {
     char[] s = "";
 
     foreach (i, v; A)
     {
         static if (i == 1)
             s = i.stringof;
     }
 
     return s;
 }
 
 void main(char[][] args)
 {
     alias foo!("test", 100) f;
 
     writefln(f());		// works				
     pragma(msg, f());	// error: cannot evaluate at compile time
 }
Probably this is a bug too. I haven't really used CTFE much yet, but here's a smaller version of the above that triggers the same error msg with dmd 1.013 char[] foo(A...)() { foreach (i, v; A) {} return ""; } pragma(msg, foo!("a")()); // error It works if you remove the index part from the foreach or change "a" to e.g. 42.
Apr 24 2007
parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Jari-Matti Mäkelä wrote:
 Max Samukha wrote:
 Why the function cannot be evaluated at compile time? Is it a bug? 

 import std.stdio;

 char[] foo(A...)()
 {
     char[] s = "";

     foreach (i, v; A)
     {
         static if (i == 1)
             s = i.stringof;
     }

     return s;
 }

 void main(char[][] args)
 {
     alias foo!("test", 100) f;

     writefln(f());		// works				
     pragma(msg, f());	// error: cannot evaluate at compile time
 }
Probably this is a bug too. I haven't really used CTFE much yet, but here's a smaller version of the above that triggers the same error msg with dmd 1.013 char[] foo(A...)() { foreach (i, v; A) {} return ""; } pragma(msg, foo!("a")()); // error It works if you remove the index part from the foreach or change "a" to e.g. 42.
Actually dmd having so many bugs in tuples and type inference makes me wonder if Walter really knows the best way to implement these. I mean many language docs just state that they use the Hindley-Milner algorithm and everything just seems to work. I'm sure these are not easy to implement, but I just had to ask this.
Apr 24 2007
parent reply Max Samukha <samukha voliacable.com> writes:
On Wed, 25 Apr 2007 02:43:24 +0300, Jari-Matti Makela
<jmjmak utu.fi.invalid> wrote:

Jari-Matti Makela wrote:
 Max Samukha wrote:
 Why the function cannot be evaluated at compile time? Is it a bug? 

 import std.stdio;

 char[] foo(A...)()
 {
     char[] s = "";

     foreach (i, v; A)
     {
         static if (i == 1)
             s = i.stringof;
     }

     return s;
 }

 void main(char[][] args)
 {
     alias foo!("test", 100) f;

     writefln(f());		// works				
     pragma(msg, f());	// error: cannot evaluate at compile time
 }
Probably this is a bug too. I haven't really used CTFE much yet, but here's a smaller version of the above that triggers the same error msg with dmd 1.013 char[] foo(A...)() { foreach (i, v; A) {} return ""; } pragma(msg, foo!("a")()); // error It works if you remove the index part from the foreach or change "a" to e.g. 42.
Actually dmd having so many bugs in tuples and type inference makes me wonder if Walter really knows the best way to implement these. I mean many language docs just state that they use the Hindley-Milner algorithm and everything just seems to work. I'm sure these are not easy to implement, but I just had to ask this.
I know absolutely nothing about how tuples and type inference can be implemented, so would you please ask this in D NG (Walter does not seem to give much attention to D.learn)? It would be interesting to know Walter's or other experts' answers. I won't post those bugs because many of them seem to be related. Hopefully, they will be fixed in the next release.
Apr 26 2007
parent bc <mi_emayl_adrez hotmail.com> writes:
On Thu, 26 Apr 2007 09:20:58 +0100, Max Samukha <samukha voliacable.com>  
wrote:

 Actually dmd having so many bugs in tuples and type inference makes me
 wonder if Walter really knows the best way to implement these. I mean
 many language docs just state that they use the Hindley-Milner algorithm
 and everything just seems to work. I'm sure these are not easy to
 implement, but I just had to ask this.
I know absolutely nothing about how tuples and type inference can be implemented, so would you please ask this in D NG (Walter does not seem to give much attention to D.learn)? It would be interesting to know Walter's or other experts' answers. I won't post those bugs because many of them seem to be related. Hopefully, they will be fixed in the next release.
i'm curious about this. doesn't hindley-milner type inference require that you implement a simple(ish) version of prolog?
May 01 2007