tag:blogger.com,1999:blog-3782954984366933851.post2223800874295233257..comments2023-06-19T01:22:31.258+12:00Comments on Steve's Software Development Blog: Have an iif() function in DelphiSteve Peacockehttp://www.blogger.com/profile/03155137500284265720noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-3782954984366933851.post-70522220409119346572016-05-08T13:58:56.081+12:002016-05-08T13:58:56.081+12:00If found Eval and as far as i remenber codeblocks:...If found Eval and as far as i remenber codeblocks:<br />DbEval({|x,y,z| myfunction_form_etc(P1,P2,P3) }); It was the most powerfull thing i've ever worked.I had made a full application<br />all parameterized from menus, tbrowse etc... all carried from <br />saved parameter files and running on fly.<br />Do you know abaou some dbeval classes in from delphi7 to XE7<br />I never heard of it!<br />It's a pittty we have nothing like it... <br />Don't you think so?Unknownhttps://www.blogger.com/profile/01106777521010835976noreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-69479049669041725862013-07-26T13:39:15.375+12:002013-07-26T13:39:15.375+12:00x := iif(not Assigned(mypointer), 0, ...x := iif(not Assigned(mypointer), 0, ...Anonymoushttps://www.blogger.com/profile/00896466127164386750noreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-48635393476466999392012-08-15T11:31:40.722+12:002012-08-15T11:31:40.722+12:00You can minimize overloads with generics:
type
...You can minimize overloads with generics:<br /><br />type<br /> iif = class<br /> public<br /> class function go(fpCondition: boolean; fpOnTrue, fpOnFalse: W): W;<br /> end;<br /><br /><br />class function iif.go(fpCondition: boolean; fpOnTrue, fpOnFalse: W): W;<br />begin<br /> if fpCondition then<br /> result := fpOnTrue<br /> else<br /> result := fpOnFalse;<br />end;<br /><br /><br />Usage:<br />iif.go(1 = 1, 'YES', 'NO')<br />iif.go( 1 = 2, 1, 0)<br /><br />:)<br /><br />Petar Georgievhttps://www.blogger.com/profile/09274855584658510126noreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-1670082493311604132010-04-28T21:52:46.020+12:002010-04-28T21:52:46.020+12:00I just implemented it using variants:
function ii...I just implemented it using variants:<br /><br />function iif(Test: boolean; TrueR, FalseR: variant): variant;<br />begin<br /> if Test then<br /> Result := TrueR<br /> else<br /> Result := FalseR;<br />end;Juanmihttp://www.binfactory.comnoreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-28768680260702058532007-08-25T02:37:00.000+12:002007-08-25T02:37:00.000+12:00If the IIF() function was created with "inline" di...If the IIF() function was created with "inline" directive (available in D2005+ i think), would that get over the limitations mentioned above?<BR/>(Since the function will be expanded inline, as if a real IF statement was written).<BR/><BR/>It's worth a try to see if it works :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-2853042727604082962007-08-24T23:49:00.000+12:002007-08-24T23:49:00.000+12:00Instead of use an overloaded version of the same f...Instead of use an overloaded version of the same function, I used a "variant" version:<BR/><BR/>function iif(cond:boolean;ifT:Variant;ifF:Variant):Variant;<BR/><BR/>This is good for Database applications where the value coming from tables are variants.SviluppoeSviluppihttps://www.blogger.com/profile/06839527702684773987noreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-45159477175138919972007-08-24T09:06:00.000+12:002007-08-24T09:06:00.000+12:00As already pointed out, both expressions are evalu...As already pointed out, both expressions are evaluated. However, it isn't just a performance problem, it's also a technical problem, you simply can't write some statements, e.g.<BR/><BR/>x := iff( mypointer=nil, 0, mypointer^.z);<BR/><BR/>will result in Access Violation if mypointer is nil. Pity.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-30443011802888916162007-08-24T07:44:00.000+12:002007-08-24T07:44:00.000+12:00Some very good posts there. I remember the Eval fu...Some very good posts there. I remember the Eval function in Clipper and used it in some tricky areas. It was nice for what it did.<BR/><BR/>Please, please take note of Jolyn Smith's comments. The IIF functions described here and in the mentioned units are not a tru, or even close relationship to a fully implemented IIF in the language itself.<BR/><BR/>I enthusiastically, jumping up and down yelling even, urge CodeGear to implement iif in the base Pascal language.<BR/><BR/>But if we are suggesting altering the base Pascal, how will that effect the "Pascal Sentence"? (see earlier blog) The Pascal Sentence is the underlying philosophy of Pascal itself. Perhaps this is why IIF was never implemented as it can be arued that it disrupts the smooth flow of language.<BR/><BR/>However, I suggest that the Pascal Sentence will not be disrupted at all. Pascal has grown well beyond the 20 odd words it used in Turbo Pascal v1. In fact the addition of the immediate if into the base Pascal language itself is well and truly overdue.Steve Peacockehttps://www.blogger.com/profile/03155137500284265720noreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-75383775543925399752007-08-24T05:29:00.000+12:002007-08-24T05:29:00.000+12:00Jolyon Smith point out a real problem with a non C...Jolyon Smith point out a real problem with a non Codegear implementation of iff(), that is that both result parameters will evaluate. Compare that to the IF statement... and I will choice the IF statement if I suspect a possible expensive performance problem.<BR/><BR/>It would be be a cheery day in the bullpen if Codegear would add an iff() function with delayed parameter evaluation.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-31699314619471936032007-08-23T18:52:00.000+12:002007-08-23T18:52:00.000+12:00Eval is evil, ask any experienced javascript progr...Eval is evil, ask any experienced javascript programmer. I'd rather be forced to do the right thing by not having eval than having it sit there and tempt me to cut corners.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-60538574465154022962007-08-23T13:49:00.000+12:002007-08-23T13:49:00.000+12:00There is also a IfThen function declared in StrUti...There is also a IfThen function declared in StrUtils that returns a string. If you use StrUtils and Math units then you could use both functions like:<BR/><BR/>str := IfThen([BoolExpr], [StrExpr1], [StrExpr2]);<BR/><BR/>or:<BR/><BR/>i := IfThen([BoolExpr], [IntExpr], [IntExpr]);<BR/><BR/>Nevertheless, as joylon said, I think it lacks of a complete set of overloaded versions. <BR/><BR/>But what could you say about an EVAL(code: string) function? this little function is an interpreter itself of whatever code in form of string that you pass to it as its parameter. This is a function of Clipper (exactly, CA-Clipper) back in the nineties. <BR/><BR/>Of course, Clipper use to embed itself in every executable. :PChava GRhttps://www.blogger.com/profile/16015602287395930873noreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-21947405477709260082007-08-23T12:59:00.000+12:002007-08-23T12:59:00.000+12:00There is also a iif() function in IdGlobal.pas, wh...There is also a iif() function in IdGlobal.pas, which is part of Indy, which is in Delphi since D6 (iirc).Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-23203505302786761512007-08-23T12:47:00.000+12:002007-08-23T12:47:00.000+12:00FYI: A Math implementation of IfThen() has been in...FYI: A Math implementation of IfThen() has been in the Delphi RTL since 7.0 I think, or perhaps earlier, but doesn't provide a complete set of overloaded versions.<BR/><BR/>Also something to be VERY cautious of when using these in Delphi is that all parameters have to be evaluated BEFORE your IIF() or IfThen() function can be called. So this sort of thing will cause problems:<BR/><BR/> count := IIF( Assigned(list), list.Count, 0);<BR/><BR/><BR/>This (I think) is different from the most oft quoted equivalent - the ? or tertiary operator in C/C++, where if memory serves, only the argument determined by the condition is evaluated.<BR/><BR/>IIF/IfThen is nice. A proper tertiary operator would be even nicer.<BR/><BR/>:)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3782954984366933851.post-64298765960459715152007-08-23T12:42:00.000+12:002007-08-23T12:42:00.000+12:00Thanks to a quick note from Dennis Gurock, Delphi ...Thanks to a quick note from Dennis Gurock, Delphi does have a similar iif function defined as IfThen(), available in the Math unit.<BR/><BR/>Math.IfThen() works in the same way as the iif() function defined here, but returns Integer, Int64, or Double, depending on the parameters given.<BR/><BR/>Thanks Dennis.Steve Peacockehttps://www.blogger.com/profile/03155137500284265720noreply@blogger.com