Jump to page: 1 214  
Page
Thread overview
Compile time function execution...
Feb 15, 2007
Walter Bright
Feb 15, 2007
Ary Manzana
Feb 15, 2007
BCS
Feb 15, 2007
Ary Manzana
Feb 15, 2007
BCS
Feb 15, 2007
Nicolai Waniek
Feb 15, 2007
Walter Bright
Feb 15, 2007
BCS
Feb 15, 2007
Walter Bright
Feb 15, 2007
BCS
Feb 15, 2007
Walter Bright
Feb 16, 2007
Russell Lewis
Feb 16, 2007
Walter Bright
Feb 16, 2007
Russell Lewis
Feb 16, 2007
Walter Bright
Feb 15, 2007
kris
Feb 15, 2007
Walter Bright
Feb 15, 2007
Walter Bright
Feb 15, 2007
Bill Baxter
Feb 15, 2007
Lutger
Feb 16, 2007
Reiner Pope
Feb 15, 2007
Sean Kelly
Feb 15, 2007
Walter Bright
Feb 15, 2007
Walter Bright
Feb 15, 2007
Walter Bright
Feb 15, 2007
Walter Bright
Feb 15, 2007
Michiel
Feb 15, 2007
Frits van Bommel
Feb 15, 2007
Michiel
Feb 15, 2007
Bill Baxter
Feb 15, 2007
Michiel
Feb 15, 2007
Walter Bright
Feb 15, 2007
Michiel
Feb 15, 2007
Walter Bright
Feb 15, 2007
Michiel
Feb 15, 2007
Walter Bright
Feb 15, 2007
Michiel
Feb 16, 2007
Walter Bright
Feb 16, 2007
Michiel
Feb 16, 2007
Walter Bright
Feb 16, 2007
Reiner Pope
Feb 16, 2007
Walter Bright
Feb 16, 2007
Reiner Pope
Feb 15, 2007
BCS
Feb 15, 2007
Michiel
Feb 15, 2007
BCS
Feb 15, 2007
Michiel
Feb 15, 2007
Michiel
Feb 16, 2007
Russell Lewis
Feb 16, 2007
Derek Parnell
Feb 16, 2007
Frits van Bommel
Feb 16, 2007
Aarti_pl
Feb 17, 2007
janderson
Feb 17, 2007
Michiel
Feb 15, 2007
Walter Bright
Feb 15, 2007
Frits van Bommel
Feb 15, 2007
Nicolai Waniek
Feb 15, 2007
Gregor Richards
Feb 15, 2007
Walter Bright
Feb 16, 2007
Max Samukha
Feb 16, 2007
Walter Bright
Feb 16, 2007
Witold Baryluk
Feb 15, 2007
Hasan Aljudy
Feb 15, 2007
Bill Baxter
Feb 15, 2007
Walter Bright
Feb 16, 2007
Bill Baxter
Feb 16, 2007
Walter Bright
Feb 16, 2007
Bill Baxter
Feb 16, 2007
Walter Bright
Feb 16, 2007
janderson
Feb 16, 2007
Walter Bright
Feb 16, 2007
Bill Baxter
Feb 16, 2007
Dave
Feb 16, 2007
BCS
Feb 16, 2007
Dave
Feb 16, 2007
Kevin Bealer
Feb 16, 2007
janderson
Feb 16, 2007
Frits van Bommel
Feb 17, 2007
Joe
Feb 15, 2007
torhu
Feb 15, 2007
Walter Bright
Feb 16, 2007
Dave
Feb 16, 2007
Michiel
Feb 16, 2007
Walter Bright
Feb 16, 2007
Frits van Bommel
Feb 16, 2007
Bill Baxter
Feb 16, 2007
Walter Bright
Feb 16, 2007
Bill Baxter
Feb 16, 2007
Walter Bright
Feb 16, 2007
Walter Bright
Feb 16, 2007
Bill Baxter
Feb 16, 2007
Bill Baxter
Feb 16, 2007
Derek Parnell
Feb 16, 2007
Derek Parnell
Feb 16, 2007
Walter Bright
Feb 16, 2007
Walter Bright
Feb 16, 2007
Frits van Bommel
Feb 16, 2007
Lionello Lunesu
Feb 16, 2007
Frits van Bommel
Feb 16, 2007
Lionello Lunesu
Feb 16, 2007
Frits van Bommel
Feb 16, 2007
Kevin Bealer
Feb 16, 2007
Walter Bright
Feb 16, 2007
Russell Lewis
Feb 16, 2007
Walter Bright
Feb 16, 2007
janderson
Feb 16, 2007
Lionello Lunesu
Feb 16, 2007
Daniel919
Feb 16, 2007
Nicolai Waniek
Feb 16, 2007
Michiel
Feb 16, 2007
Brian Byrne
Feb 16, 2007
Frits van Bommel
Feb 20, 2007
J Duncan
Feb 20, 2007
Frits van Bommel
Feb 24, 2007
Serg Kovrov
Feb 24, 2007
Frits van Bommel
Feb 24, 2007
Serg Kovrov
Feb 24, 2007
Tyler Knott
Feb 24, 2007
Serg Kovrov
Feb 25, 2007
janderson
Feb 25, 2007
Walter Bright
February 15, 2007
... is now in DMD 1.006. For example:

> -------------------------------------------
> import std.stdio;
> 
> real sqrt(real x)
> {
>    real root = x / 2;
>    for (int ntries = 0; ntries < 5; ntries++)
>    {
>        if (root * root - x == 0)
>            break;
>        root = (root + x / root) / 2;
>    }
>    return root;
> }
> 
> void main()
> {
>    static x = sqrt(10);   // woo-hoo! set to 3.16228 at compile time!
>    writefln("%s, %s", x, sqrt(10));  // this sqrt(10) runs at run time
> }
> ------------------------------------------

This should obsolete using templates to compute values at compile time.
February 15, 2007
Walter Bright escribió:
> ... is now in DMD 1.006. For example:
> 
>> -------------------------------------------
>> import std.stdio;
>>
>> real sqrt(real x)
>> {
>>    real root = x / 2;
>>    for (int ntries = 0; ntries < 5; ntries++)
>>    {
>>        if (root * root - x == 0)
>>            break;
>>        root = (root + x / root) / 2;
>>    }
>>    return root;
>> }
>>
>> void main()
>> {
>>    static x = sqrt(10);   // woo-hoo! set to 3.16228 at compile time!
>>    writefln("%s, %s", x, sqrt(10));  // this sqrt(10) runs at run time
>> }
>> ------------------------------------------
> 
> This should obsolete using templates to compute values at compile time.

This is really great, Walter! Congratulations!

I'm always thinking that D is focused on great runtime performance while having great expresiveness, which is awsome. I can't believe computers have evolved that much... and still we have to wait 10 o 20 seconds for a program to load :-(

A question: is there anyway the compiler can tell the user if a certain function can be executed at compile time? Those six rules may be hard to memorize and while coding sensitive code it would be great to ask the compiler "Is the function I'm writing can be executed at compile time?". Otherwise it's just a guess.
February 15, 2007
Walter Bright wrote:
> ... is now in DMD 1.006. For example:
> 
>> -------------------------------------------
>> import std.stdio;
>>
>> real sqrt(real x)
>> {
>>    real root = x / 2;
>>    for (int ntries = 0; ntries < 5; ntries++)
>>    {
>>        if (root * root - x == 0)
>>            break;
>>        root = (root + x / root) / 2;
>>    }
>>    return root;
>> }
>>
>> void main()
>> {
>>    static x = sqrt(10);   // woo-hoo! set to 3.16228 at compile time!
>>    writefln("%s, %s", x, sqrt(10));  // this sqrt(10) runs at run time
>> }
>> ------------------------------------------
> 
> 
> This should obsolete using templates to compute values at compile time.


Good one. Can you perhaps explain what the execution mechanism is? That aspect is much more interesting to some of us geeks <g>
February 15, 2007
Ary Manzana wrote:
> A question: is there anyway the compiler can tell the user if a certain function can be executed at compile time? Those six rules may be hard to memorize and while coding sensitive code it would be great to ask the compiler "Is the function I'm writing can be executed at compile time?". Otherwise it's just a guess.

int[foo()] should do it, I think, or temp!(foo()).

Both hack, but...
February 15, 2007
Walter Bright wrote:
> This should obsolete using templates to compute values at compile time.

For contrast, compare with the C++ proposal:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1972.pdf
February 15, 2007
Ary Manzana wrote:
> A question: is there anyway the compiler can tell the user if a certain function can be executed at compile time? Those six rules may be hard to memorize and while coding sensitive code it would be great to ask the compiler "Is the function I'm writing can be executed at compile time?". Otherwise it's just a guess.

The way to tell is it gives you an error if you try to execute it at compile time and it cannot be.

Don't bother trying to memorize the rules, if you follow instead the rule "imagine regular compile time constant folding and extend it to functions" and you'll be about 99% correct.
February 15, 2007
kris wrote:
> Good one.

I thought you'd appreciate it because it involves NO new syntax. It just  makes things work that were errors before.

> Can you perhaps explain what the execution mechanism is? That aspect is much more interesting to some of us geeks <g>

The dirty deed is done in interpret.c. It basically just makes more powerful the existing constant folding code.
February 15, 2007
BCS escribió:
> Ary Manzana wrote:
>> A question: is there anyway the compiler can tell the user if a certain function can be executed at compile time? Those six rules may be hard to memorize and while coding sensitive code it would be great to ask the compiler "Is the function I'm writing can be executed at compile time?". Otherwise it's just a guess.
> 
> int[foo()] should do it, I think, or temp!(foo()).
> 
> Both hack, but...

It would be nice to have something like this in phobos:

-------------------------------------------------
import ... ?

int square(int i) { return i * i; }

static assert (isCompileTimeExecution(square());
-------------------------------------------------

so that if the function is changed you can still assert that or know that you've lost it.
February 15, 2007
Walter Bright wrote:
> ... is now in DMD 1.006. For example:
> 
>> -------------------------------------------
>> import std.stdio;
>>
>> real sqrt(real x)
>> {
>>    real root = x / 2;
>>    for (int ntries = 0; ntries < 5; ntries++)
>>    {
>>        if (root * root - x == 0)
>>            break;
>>        root = (root + x / root) / 2;
>>    }
>>    return root;
>> }
>>
>> void main()
>> {
>>    static x = sqrt(10);   // woo-hoo! set to 3.16228 at compile time!
>>    writefln("%s, %s", x, sqrt(10));  // this sqrt(10) runs at run time
>> }
>> ------------------------------------------
> 
> This should obsolete using templates to compute values at compile time.

This is a development of epic proportions.

There is a need for a couple of ancillary features. Most importantly, a constant must be distinguishable from a variable. Consider the example of regex from our correspondence:

bool b = regexmatch(a, "\n$");

vs.

char[] pattern = argv[1];
bool b = regexmatch(a, pattern);

You'd want to dispatch regexmatch differently: the first match should be passed to compile-time code that at the end of the day yields:

bool b = (a[$-1] == '\n');

while the second should invoke the full-general dynamic pattern matching algorithm since a dynamic pattern is used.


Andrei
February 15, 2007
Walter Bright wrote:

> ... is now in DMD 1.006. For example:
> 
>> -------------------------------------------
>> import std.stdio;
>>
>> real sqrt(real x)
>> {
>>    real root = x / 2;
>>    for (int ntries = 0; ntries < 5; ntries++)
>>    {
>>        if (root * root - x == 0)
>>            break;
>>        root = (root + x / root) / 2;
>>    }
>>    return root;
>> }
>>
>> void main()
>> {
>>    static x = sqrt(10);   // woo-hoo! set to 3.16228 at compile time!
>>    writefln("%s, %s", x, sqrt(10));  // this sqrt(10) runs at run time
>> }
>> ------------------------------------------
> 
> This should obsolete using templates to compute values at compile time.


That's a great feature! A couple of questions, if I may:

* Can sqrt still call another function?

* Does it still work if classes or structs are involved in any way? What about enums? What about arrays?

* Why is the second sqrt(10) run at runtime? In theory it's still a constant, right? Is this something that will work in a later version?

Congrats on the new feature!
« First   ‹ Prev
1 2 3 4 5 6 7 8 9 10 11