Thread overview
What is wrong here? Error: cannot cast int to SYSTEMTIME
Oct 23, 2007
Spacen Jasset
Oct 23, 2007
BCS
Oct 23, 2007
Spacen Jasset
Oct 23, 2007
BCS
October 23, 2007
Digital Mars D Compiler v1.015

Neither of these lines will compile and give: Error: cannot cast int to SYSTEMTIME

without a line number. How might I get this to work?


import std.date;

//long utc_time1 = std.date.getUTCtime();

void main()
{
	//static long utc_time1 = std.date.getUTCtime();
}

Regards,
Jason
October 23, 2007
Reply to Spacen,

> Digital Mars D Compiler v1.015
> 
> Neither of these lines will compile and give: Error: cannot cast int
> to SYSTEMTIME
> 
> without a line number. How might I get this to work?
> 
> import std.date;
> 
> //long utc_time1 = std.date.getUTCtime();
> 
> void main()
> {
> //static long utc_time1 = std.date.getUTCtime();
> }
> Regards,
> Jason

For the first line, do this:

utc_time1 = std.date.getUTCtime();
static this(){long utc_time1 = std.date.getUTCtime();}

For the second drop the static (if it isn't actualy in main, you will have to do somthing else)

The issue is that D doesn't allow runtime initializers for non stack variables.

For the static function variable one you might be able to get away with this:

template T(char[] str, A...)
{
  long v;
  static this(){v = mixin(str);}
}


void fn()
{
  alias T!("std.date.getUTCtime()", __FILE__, __LINE__).v utc_time1;
}


there will be one version of T!(...).v for each distinct "...". Note, if you put this in a templated function, make sure you put pass on the template args or things get strange.


October 23, 2007
I have seen this error before.  It was a bug in the compiler (http://d.puremagic.com/issues/show_bug.cgi?id=1300).  Try upgrading to latest, I have dmd 1.021 (1.019 fixed this bug).

Once you do upgrade, you will find that you get a much more descriptive error, because BCS is right, static initializers must be evaluated at compile time.  Follow his instructions to get it working how you want.

-Steve

"Spacen Jasset" wrote
> Digital Mars D Compiler v1.015
>
> Neither of these lines will compile and give: Error: cannot cast int to SYSTEMTIME
>
> without a line number. How might I get this to work?
>
>
> import std.date;
>
> //long utc_time1 = std.date.getUTCtime();
>
> void main()
> {
> //static long utc_time1 = std.date.getUTCtime();
> }
>
> Regards,
> Jason


October 23, 2007
BCS wrote:
> Reply to Spacen,
> 
>> Digital Mars D Compiler v1.015
>>
>> Neither of these lines will compile and give: Error: cannot cast int
>> to SYSTEMTIME
>>
>> without a line number. How might I get this to work?
>>
>> import std.date;
>>
>> //long utc_time1 = std.date.getUTCtime();
>>
>> void main()
>> {
>> //static long utc_time1 = std.date.getUTCtime();
>> }
>> Regards,
>> Jason
> 
> For the first line, do this:
> 
> utc_time1 = std.date.getUTCtime();
> static this(){long utc_time1 = std.date.getUTCtime();}
> 
> For the second drop the static (if it isn't actualy in main, you will have to do somthing else)
> 
> The issue is that D doesn't allow runtime initializers for non stack variables.
> 
> For the static function variable one you might be able to get away with this:
> 
> template T(char[] str, A...)
> {
>   long v;
>   static this(){v = mixin(str);}
> }
> 
> 
> void fn()
> {
>   alias T!("std.date.getUTCtime()", __FILE__, __LINE__).v utc_time1;
> }
> 
> 
> there will be one version of T!(...).v for each distinct "...". Note, if you put this in a templated function, make sure you put pass on the template args or things get strange.
> 
> 
Ah ok. I need to read up on this. The code I was intending to use in that manner was for testing. Presumably the way is to create a class and use objects.

Thanks for the assistance.
October 23, 2007
Reply to Spacen,

> BCS wrote:
> 
>> For the first line, do this:
>> 
>> utc_time1 = std.date.getUTCtime();
>> static this(){long utc_time1 = std.date.getUTCtime();}
>> For the second drop the static (if it isn't actualy in main, you will
>> have to do somthing else)
>> 
>> The issue is that D doesn't allow runtime initializers for non stack
>> variables.
>> 
>
> Ah ok. I need to read up on this. The code I was intending to use in
> that manner was for testing. Presumably the way is to create a class
> and use objects.
> 

What do objects have to do with it? I might not be understanding what you want to do.

> Thanks for the assistance.
>