DMD 1.007 release

Max Samukha samukha at voliacable.com
Fri Feb 23 23:59:58 PST 2007


On Fri, 23 Feb 2007 17:00:55 -0700, Hasan Aljudy
<hasan.aljudy at gmail.com> wrote:

>
>
>Max Samukha wrote:
>> On Thu, 22 Feb 2007 14:20:30 -0700, Hasan Aljudy
>> <hasan.aljudy at gmail.com> wrote:
>> 
>>>
>>> Walter Bright wrote:
>>>> Hasan Aljudy wrote:
>>>>> Is there a reason why this shouldn't work?! It seems to me this is the 
>>>>> most basic usage for mixin & compile time functions.
>>>> Try putting the definition of getstruct() before the mixin.
>>> What about this one?
>>>
>>> ---------------
>>> dchar[] testd( dchar[] input )
>>> {
>>> 	if( input[3..5] != "rt" )
>>> 	{
>>> 		return input[1..3];
>>> 	}
>>> 	return "my";
>>> }
>>>
>>> void main()
>>> {
>>> 	static x = testd( "hello" );
>>> }
>>> ----------------
>>>
>>> it says:
>>>  Error: cannot evaluate testd("hello") at compile time
>> 
>> I've been racking my brain over the problem too. It seems like string
>> comparison expressions are not evaluable at compile time. 
>> 
>> input[3] != 'r' || input[4] != ''t" should work
>> 
>> you can also make your own compare function
>> 
>
>What I was pointing out was that slicing doesn't seem to work.

Slicing works. Comparisons is what makes your function unevaluable at
compiletime. Try to use a function to compare slices. Something like
this:

bool isEqual(dchar[] str1, dchar[] str2)
{
	if (str1.length != str2.length)
		return false;

	for (int i = 0; i < str1.length; i++)
		if (str1[i] != str2[i])
			return false;
	
	return true;
}

dchar[] testd( dchar[] input )
{
	if( !isEqual(input[3..5], "rt"))
 	{
 		return input[1..3];
 	}
 	return "my";
 }

void main()
{
	static x = testd( "hello" );
}

Trickier workarounds might exist
   



More information about the Digitalmars-d-announce mailing list