Howard Bullock
(KiX Supporter)
2003-06-02 04:16 AM
IIF enhancement

It appears that when one uses an IIF(condition,Exp1,Exp2), both Exp1 and Exp2 are evaluated when only one should be based on the condition.

Ruud, could you please alter the current IIF code so that only one expression is evaluted after the condition is checked?


Sealeopard
(KiX Master)
2003-06-02 04:21 AM
Re: IIF enhancement

Howard, already been discussed in iif() stack overflow exception "problem"? and Ruud's answer was basically "It's by design" [Smile]

Howard Bullock
(KiX Supporter)
2003-06-02 04:44 AM
Re: IIF enhancement

Bummer [Frown] Thanks for letting me know.

LonkeroAdministrator
(KiX Master Guru)
2003-06-02 11:20 AM
Re: IIF enhancement

although, the design could be changed to work as:

code:
function iif2($condition,$exp1,$exp2)
dim $exp
$exp=$exp1,$exp2
exit execute($iif2 = $exp[not not $condition])
endfunction

or is that too complex?

[ 02. June 2003, 12:22: Message edited by: Lonkero ]


Richard H.Administrator
(KiX Supporter)
2003-06-03 12:16 AM
Re: IIF enhancement

quote:
or is that too complex?
Not complex, but it won't always work.

One problem is that IIF() returns arbitrary variable types, while your solution appears to be limited to numeric types.

The second problem is variable scope. You will either need to parse variables at the time you call IIF2() in which case it has the same problems as the existing IIF, or you will have to make the variables global.

You can get around the arbitrary type by making an assignment in the Execute
code:
Exit Execute("$$IIF2="+$exp[not not $condition])

But the variable scope problem is a tougher nut to crack.


LonkeroAdministrator
(KiX Master Guru)
2003-06-03 12:28 AM
Re: IIF enhancement

indeed I forgot that setting of $iif2.

but don't understand what you are wondering about vartypes.
iif2 does not care about them.

don't know what you mean with scope either, as what you put into arg, has the scope of the udf.
so, can't see a problem there.
{edit}
mm...
now I think I got what you ment.
anyway, see my end comment as it says it really clearly [Wink]
this was just kinda pseudo-presentation that it can be done [Razz]
{/edit}

and as final, I didn't mean this as solution.
no, I just wanted to show how easy it would actually be to re-design the iif of kix and make it actually usefull.


Richard H.Administrator
(KiX Supporter)
2003-06-03 12:48 AM
Re: IIF enhancement

Other languages which support immediate ifs do it by using a conditional expression, introduced by a ternary (three-part) operator. In that case, because the conditional expression is a language construct rather than a function the result expressions can be conditionally evaluated.

Where the solution is provided by a function, whether internal or UDF, you will always have to evaluate both result expressions regardless of the result of the conditional test.


NTDOCAdministrator
(KiX Master)
2004-07-26 10:10 PM
Re: IIF enhancement

Sorry to reply to such an old thread, but since a recent post was created that referenced this one, which referenced another one from the OLD board I thought I would provide the current link that Jens referenced so others don't have to search for it.

iif() stack overflow exception "problem"?
#79079 - 08/01/2003 09:27
http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=79079

This post was referenced from a newer post by Bryce here

IIF() is executing both conditions
http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=122731