www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - compile-time initialization of objects need?

reply Weed <resume755 mail.ru> writes:
In fact, the D is not contain a mechanism for compile-time
initialization of objects. Maybe add?

For example, that would be very usable for any objects which have
mathematical primitives.

Example:

struct S
{
    float[10] array;

    this( float f )
    {
        array[0] = f;
    }
}

void main()
{
    S a = S(8); // ok
    static S b = S(8); // error
}
Dec 26 2008
parent reply Christopher Wright <dhasenan gmail.com> writes:
Weed wrote:
 In fact, the D is not contain a mechanism for compile-time
 initialization of objects. Maybe add?
 void main()
 {
     S a = S(8); // ok
     static S b = S(8); // error
 }
You want syntactic sugar for two things: // example 1 void foo () { static S s; static bool s_initialized = false; if (!s_initialized) { s_initialized = true; s = S(8); } } // example 2 // module/class level S s; static this () { s = S(8); } Neither of these need to happen at compile time.
Dec 27 2008
parent reply Weed <resume755 mail.ru> writes:
Christopher Wright пишет:
 Weed wrote:
 In fact, the D is not contain a mechanism for compile-time
 initialization of objects. Maybe add?
 void main()
 {
     S a = S(8); // ok
     static S b = S(8); // error
 }
You want syntactic sugar for two things: // example 1 void foo () { static S s; static bool s_initialized = false; if (!s_initialized) { s_initialized = true; s = S(8); } } // example 2 // module/class level S s; static this () { s = S(8); } Neither of these need to happen at compile time.
It not syntactic sugar. I suggest not to waste time at all performance on run-time initialization of objects and check of side conditions on a course of performance of the program.
Dec 28 2008
parent reply Christopher Wright <dhasenan gmail.com> writes:
Weed wrote:
 It not syntactic sugar. I suggest not to waste time at all performance
 on run-time initialization of objects and check of side conditions on a
 course of performance of the program.
Sorry, syntactic sugar and some minor optimizations.
Dec 28 2008
parent reply Weed <resume755 mail.ru> writes:
Christopher Wright пишет:
 Weed wrote:
 It not syntactic sugar. I suggest not to waste time at all performance
 on run-time initialization of objects and check of side conditions on a
 course of performance of the program.
Sorry, syntactic sugar and some minor optimizations.
These of optimization are very important when the program contains many objects for mathematics. In adjacent thread wish to take out complex numbers in library, for example. Those who used static initialization of complex types can want to use it and with new library.
Dec 28 2008
parent Christopher Wright <dhasenan gmail.com> writes:
Weed wrote:
 Christopher Wright пишет:
 Weed wrote:
 It not syntactic sugar. I suggest not to waste time at all performance
 on run-time initialization of objects and check of side conditions on a
 course of performance of the program.
Sorry, syntactic sugar and some minor optimizations.
These of optimization are very important when the program contains many objects for mathematics.
If the object is mutable, you still need to get it into writable memory. If the computation is cheap enough that it's reasonable to do at compile time, copying will be a major cost. Therefore this optimization is not very important.
Dec 28 2008