POV-Ray : Newsgroups : povray.newusers : if inside a macro syntax (if...else) : Re: if inside a macro syntax (if...else) Server Time
30 Jul 2024 00:24:01 EDT (-0400)
  Re: if inside a macro syntax (if...else)  
From: Mike Williams
Date: 30 Dec 2004 01:45:56
Message: <R9s3QCAcQ60BFw+k@econym.demon.co.uk>
Wasn't it Rafal 'Raf256' Maj who wrote:
>
>Hi,
>syntax like
>
>#local size = big ? 100 : 50;
>
>(from C "operator?" ) isnt working, and writting it normaly as:
>
>#if (big) #local size=100; #else #local size=50; #end
>
>is quite long (I like to keep my syntax short). So I wanted to write a tiny 
>IfElse function, like:
>
>#macro IfElse(A,B,C) #if (A) 55 #else 30 #end #end
>
>#local DD = IfElse(variant_Wide, 50, 100);
>
>but it gaves a pares error.
>
>I replaced it with good sytnax:
>  #macro IfElse(A,B,C) select(A-1,C,B,C) #end
>that do work, but just for curiosity, how to make previous syntax work 
>also?


See Neil Kolban's reply for the correct syntax for using the ? operator.

Here's an explanation of why your first attempt failed and your second
one succeeded.

You need to stop thinking of macros as if they were functions.

In this particular case you could have used a function, like this:
#declare IFELSE=function(A,B,C){select((A=0),B,C)}
#local DD = IFELSE(variant_Wide, 50, 100); 

When you use a function, the calculations are performed inside the
function at run time and a value is returned. So, at parse time, the
"IFELSE(...)" looks syntactically like a variable and the syntax is
correct. #local DD = <variable>;

When you use a macro, POV replaces the call at parse time with a copy of
the *code* contained within the macro. The macro itself is not executed
independently. In your fist case, unrolling the macro produces

        #local DD = #if (variant_Wide) 55 #else 30 #end;

Which you can see is syntactically incorrect.

In the second case, unrolling the macro produces

        #local DD = select(A-1,C,B,C);

Which is syntactically correct.

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.