Page 2 of 2 <12
Topic Options
#79399 - 2003-01-08 11:59 PM Re: iif() stack overflow exception "problem"?
Lee Wilmott Offline
Starting to like KiXtart

Registered: 2002-09-17
Posts: 106
Loc: Bristol, UK
The If ... Else ... statement may well be quicker; I was just thinking (maybe that's the problem) that my exp() function would be a good test for the new iif() function.

You know what it is like, a new function is introduced to KiX - you've gotta have a look, haven't you?!?!?

Like you Shawn, I would only use iif() for the more simplistic task. However, the reason for posting my original post is that I thought that the returned result was incorrect and that it may have bigger, more important consequences.

I must admit, I'm not sure that after reading your comments that this is any clearer for me!!!

I agree with you Shawn, the iif() function is merely...

code:
Function IIF($x,$y,$z)
If $x
$IIF=$y
Else
$IIF=$z
EndIf
EndFunction

But seeing as this is all my exp() function contains is the if ... else ... statement, so why can't this statement be replaced with iif()?

Although, I believe you are correct; logically speaking this is exactly what iif() does, however programatically I would have to argue that it is different!

Am I missing something?

Lee

Top
#79400 - 2003-01-09 12:11 AM Re: iif() stack overflow exception "problem"?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22298
Loc: OK
you can replace.
but as you put a function into the parameter, it will be scanned before the iif is tested.

for shawns example:
code:
$x = iif(1=1,FuncOne(),FuncTwo())

function FuncOne()
?"FuncOne..."
endfunction

function FuncTwo()
?"FuncTwo..."
endfunction

it is translated to:
code:
$x = iif(1=1,?"FuncOne...",?"FuncTwo...")

and then executed.
say you have nested function which calls itself like you had.
I'm sure what it becomes.
it keeps adding itself to the parameters until the stack gets filled.

like you probably know, expressions are executed in bracket-style manner.

this is different with commands.
command does something and if is conditional command.

mmm...
to make iif-function work as "expected", ruud would need to override normal syntax rules for it.

making any sense?
_________________________
!

download KiXnet

Top
#79401 - 2003-01-09 09:39 AM Re: iif() stack overflow exception "problem"?
Lee Wilmott Offline
Starting to like KiXtart

Registered: 2002-09-17
Posts: 106
Loc: Bristol, UK
I'm with you!

Sorry...It was late last night when I posted my last message!

"Speak" soon,

Lee

Top
#79402 - 2003-01-09 05:55 PM Re: iif() stack overflow exception "problem"?
Anonymous
Unregistered


First of all, let me say that I was really excited to see the true community-power at work in this thread. The initial post was intriguing, and your combined responses have produced the exact cause and appropriate work arounds. Makes me feel proud to be part of this community!

And, as a result of your efforts, my response can be short: the phenomenon is indeed caused by the fact that all parameters of a function are always evaluated. Changing this behaviour would be non-trivial, and might break downward compatibility.

Basically, IIF() is just a bit of 'syntactical candy' which can make certain scripts shorter. If readability and performance are your main concern, good old IF is probably a better choice.

Thanks to all the participants, and keep scripting!

Ruud

Top
#79403 - 2003-01-09 06:11 PM Re: iif() stack overflow exception "problem"?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11162
Loc: Boston, MA, USA
So, basically
code:
$a=10
if $a<10
$b='a is smaller than 10'
else
$b='a is not smaller than 10'
endif

could be a candidate for the IIF as in
code:
$a=10
$b=iif($a<10,'a is smaller than 10','a is not smaller than 10')

but anything involving more complicated things like function evaluations would be off-limits.

You might need to update the Manual to make this clear.

[ 09. January 2003, 18:14: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#79404 - 2003-01-10 10:16 AM Re: iif() stack overflow exception "problem"?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4945
Loc: Leatherhead, Surrey, UK
Agreed, the manual should explicitly state the situations in which IIF() should be used, and the pitfalls of having the expressions always evaluated.

It caught me out at first as coming from a mainly 'C' background I am used to the ternary "?:" operator which appears to work in a similar way. The form is:
quote:
(condition) ? (True-expression) : (False-expression);
The difference is that this is a language construct, and only the expression which corresponds to the result of the condition expression is evaluated.

The fact that IIF() is a function implies that the parameters must be evaluated first, but it will save some headaches if it is spelled out explicitly in the documentation.

Top
#79405 - 2003-01-11 12:34 AM Re: iif() stack overflow exception "problem"?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22298
Loc: OK
can't say that I agree.
there is no place I've found that it's better to use iif.
mainly because I want to run stuff by speed.

there is no place where I would recommend it.

anyway, about adding some words to the manual to clarify that the parameters are eveluated will decrease the confusion.
_________________________
!

download KiXnet

Top
#79406 - 2003-01-10 04:24 PM Re: iif() stack overflow exception "problem"?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11162
Loc: Boston, MA, USA
The only place in my UDFs where I could see potential use of IIF is to simplify the following type of IF construct:
code:
; example
if @INWIN=1
$os='Windows NT/2000/XP'
else
$os='Windows 9x'
endif
; replaced example
$os=IIF(@INWIN=1,'Windows NT/2000/XP','Windows 9x')

and similar plain value-assigning IF constructs.
_________________________
There are two types of vessels, submarines and targets.

Top
#79407 - 2003-01-10 04:28 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I think IIF's are ok as long as one fully understands what one is doing - which I didn't - i thought of IIF more along the lines of the tenary IF that Richard outlined. Not the same beast at all.
Top
#79408 - 2003-01-10 04:40 PM Re: iif() stack overflow exception "problem"?
Lee Wilmott Offline
Starting to like KiXtart

Registered: 2002-09-17
Posts: 106
Loc: Bristol, UK
I totally agree with you shawn...
quote:
IIF's are ok as long as one fully understands what one is doing
Using iif() is fine...as long as you understand what it is doing and how it is doing it!

Lee

Top
Page 2 of 2 <12


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 172 anonymous users online.
Newest Members
FlyHorton, mschwj, Barchester, Evgeniy, kvanacker
17498 Registered Users

Generated in 0.036 seconds in which 0.012 seconds were spent on a total of 13 queries. Zlib compression enabled.