:? in templates

Bill Baxter wbaxter at gmail.com
Wed Nov 18 03:31:11 PST 2009


On Wed, Nov 18, 2009 at 3:16 AM, retard <re at tard.com.invalid> wrote:
> Wed, 18 Nov 2009 03:10:57 -0800, Bill Baxter wrote:
>
>> Didn't this used to work?
>>
>> template factorial(int i) {
>>     enum factorial = (i==0) ? 1 : i*factorial!(i-1);
>> }
>>
>> With DMD 2.036 I'm getting:
>>   Error: template instance factorial!(-495) recursive expansion
>>
>> Seems like it expands both branches regardless of the condition. And
>> seems to me like it shouldn't.
>
> There's probably a confusion here. It evaluates lazily the value of
> factorial!(), but its type (which happens to be infinitely recursive must
> be evaluated eagerly in order to infer the type of the ternary op.

That makes sense.  I guess the ?: op is defined to do that in all
cases.  Might be nice though if it didn't do that in cases where the
condition was statically known.  Or if we just had a separate "static
if" version of ?:

--bb



More information about the Digitalmars-d mailing list