make (a < b < c) illegal?

Tom S h3r3tic at remove.mat.uni.torun.pl
Thu Feb 8 03:50:15 PST 2007


Derek Parnell wrote:
> On Thu, 08 Feb 2007 12:27:58 +0100, Tom S wrote:
> 
>> Michiel wrote:
>>>> I give up. I can't work out how to create this functionality
>>>> for an arbitary number of arguments.
>>> You need to use recursion, not loops. I'm not sure how the syntax works exactly,
>>> but it might look something like this:
>>>
>>> bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) {
>>>     return (first < second) && ordered(second, rest);
>>> }
>> How about:
>>
>>
>> bool ascending(T ...)(T t) {
>> 	static if (t.length > 0) {
>> 		foreach (i, x; t[0..$-1]) {
>> 			if (x >= t[i+1]) return false;
>> 		}
>> 	}
>> 	return true;
>> }
>>
>>
>> void main() {
>> 	assert (ascending(1, 2, 3, 4));
>> 	assert (ascending(1, 2));
>> 	assert (ascending(1.2, 2.1, 3.5, 4.7, 8.6));
>> 	assert (!ascending(1.2, 1.0));
>> 	assert (!ascending(1.2, 1.5, 0.5));
>> 	assert (ascending(1));
>> 	assert (ascending());
>> }
> 
> Yes this works, but I don't know why. Is the 'foreach' executed at compile
> time or run time?

Both :D It executes at runtime but is completely unrolled, afaics.


> If compile time, where is this documented? Its not in the
> template docs, nor the conditional compilation page, or on the statements
> page. 

Here's some stuff: http://digitalmars.com/d/tuple.html



More information about the Digitalmars-d mailing list