Wandering OT, starttime execution

Derek Parnell derek at psych.ward
Sun May 14 22:49:28 PDT 2006


On Sun, 14 May 2006 16:43:08 -0700, Kyle Furlong wrote:

> Chad J wrote:
>> Derek Parnell wrote:
>>>
>>>
>>> There are already precedents for this type of coding. Not only the  
>>> 'unittest' idea but 'scope()' also allows us to physically separate 
>>> code  that is executed linearly. So the concept is not foreign to D or 
>>> Walter.
>>>
>> 
>> While I'm at it, I'd also like to see the ability to set a variable to 
>> the result of some function at starttime, like so:
>> 
>> int[] squares = initSquares();
>> 
>> int[] initSquares()
>> {
>>     int[] result = new int[20];
>>     for ( int i = 0; i < 20; i++ )
>>         result[i] = i * i;
>>     return result;
>> }
>> 
>> void main()
>> {
>>     char[] testStr;
>>     for ( int i = 0; i < squares.length; i++ )
>>         testStr ~= toString(i)~" squared is "~toString( squares[i] ) ~ 
>> "\n";
>> 
>>     writefln( testStr );
>> }
>> 
>> This is something I used in C# and enjoyed.  Any particular reason this 
>> doesn't exist already?  Maybe I am supposed to write my code a different 
>> way, if so please enlighten me.  My current workaround is to use static 
>> ctors, but that doesn't seem as readable to me.
> 
> This is the natural functional area for template functions.
> 
> Simply change your code to this:
> 
> int[20] squares = initSquares!(int, 20)();
> 
> template initSquares(T, int C)
> {
>      T[] initSquares()
>      {
>           T[C] result;
>           for ( int i = 0; i < C; i++ )
>               result[i] = i * i;
>           return result;
>      }
> }

Have you actually tried this, Kyle? 

I understood the issue to be that the only way that D allows one to call a
function to initialize a module-scoped static array is during the 'static
this()' function. However this means that the declaration and the
initialization code are physically separated. I think the OP wanted a
mechanism that had the initialization code and the declaration code to be
together.

Your response was illustrating templates but that is not relevant to the
problem at hand.

The best I think that one can come up using templates is ...

// --------------- code starts -----------
import std.string;
import std.stdio;

int[] squares;
static this()
{
    squares = initSquares!(int, 20);
}

template initSquares(T, int C)
{
     T[] initSquares()
     {
          T[] result = new T[C];
          foreach ( int i, inout T item; result )
              item = i * i;
          return result;
     }
}

void main()
{
    for ( int i = 0; i < squares.length; i++ )
        writefln("%s squared is %s", i, squares[i]);
}
// --------------- code ends -----------

Still the declaration and initialization are separated. And if one wants to
use something other than 'int' you have to change two lines rather than
one.

What would be *nice* is to have D see ...

  int squares = initSquares!(int, 20);

at the module-level and automatically generate the initialization code to
go into the module's ctor.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
15/05/2006 3:28:41 PM



More information about the Digitalmars-d mailing list