| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
December 26, 2008 compile-time initialization of objects need? | ||||
|---|---|---|---|---|
| ||||
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
}
| ||||
December 28, 2008 Re: compile-time initialization of objects need? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Weed | 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.
| |||
December 28, 2008 Re: compile-time initialization of objects need? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | 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.
| |||
December 28, 2008 Re: compile-time initialization of objects need? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Weed | 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.
| |||
December 28, 2008 Re: compile-time initialization of objects need? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | 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.
| |||
December 29, 2008 Re: compile-time initialization of objects need? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Weed | 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.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply