导航:
论坛 -> DELPHI技术
斑竹:liumazi,sephil
作者:
2022/9/22 4:59:00
标题:
加入我的收藏
楼主:
Math.IfThen(True,1,2); //1 StrUtils.IfThen(True, 'A', 'B');//A IdGlobal.iif(True, 1, 2);//1 IdGlobal.iif(True, 'A', 'B');//A IdGlobal.iif(True, True, False);//True
----------------------------------------------
相信自己,若自己都不相信,那还有谁可信。
作者:
2022/9/22 6:58:25
1楼:
if error=error then 'is a error' :)
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
2022/9/22 12:42:44
2楼:
Math.IfThen不会做短路处理, IfThen(Flag,A,B) 会先把A和B都算出来 有时候Flag=True的时候B不方便计算 这对习惯了C的?:来说有点痛苦
----------------------------------------------
-
作者:
tiez (骑牛夜旅)
★☆☆☆☆
-
普通会员
2022/9/23 13:48:08
3楼:
那为啥不自己写个?
----------------------------------------------
-
作者:
2022/9/23 15:49:58
4楼:
delphi 实现不了 a = true ? 1 : 2; 不管自己怎么些,都实现不了,这个是编译器层面要解决的
----------------------------------------------
-
作者:
2022/9/23 22:29:17
5楼:
TIfThenHelper = record helper for integer function ifThen<T>(right: integer; trueObj, falseObj:T):T; end; function TIfThenHelper.ifThen<T>(right: integer; trueObj, falseObj: T): T; begin if (self = right) then result:=trueObj else result:=falseObj; end; 应该可以运行: Writeln(Integer(9).ifThen<string>(10,'true','false'));
----------------------------------------------
-
作者:
2022/9/25 10:48:36
6楼:
不支持三目运算
----------------------------------------------
-
作者:
2022/9/25 11:11:49
7楼:
function 三目运算<T>(逻辑:boolean;真值,假值:T):T;
----------------------------------------------
z@S7
作者:
2022/9/25 12:23:52
8楼:
三目运算的核心是,符合条件才会调用代码 true ? a : b 为真时,才会调用a,不会调用b(假如b是个函数,不会触发调用)
----------------------------------------------
-
作者:
hs_kill (lzl_17948876)
★☆☆☆☆
-
普通会员
2022/9/26 11:29:09
9楼:
math的ifthen因为是传递实参, 所以会在调用前先把true和false的结果都计算完毕再调用 delphi没现成的东西(这玩意属于语法级别的, 只是写起来比自己写个if else语法更简洁) 如果一定要在delphi里实现ifthen的调用, 可以用匿名函数来实现 当然, 问题也显而易见, 这语法极其拖沓冗余, 反正我是完全想象不出来会有再什么情况下需要实现这样的语法 function _IfThen(AKey: Boolean; ATrue, AFalse: TFunc<Single>): Single; begin if AKey then Result := ATrue else Result := AFalse; end; procedure TForm1.Button1Click(Sender: TObject); var a, b: Integer; begin a := 1; b := 0; Caption := _IfThen(True, function:single begin result := a + b; end, function: single begin result := a / b; end ).ToString; end;
----------------------------------------------
http://www.cnblogs.com/lzl_17948876/
作者:
2022/9/26 12:15:49
10楼:
用 helper 的方式也可以实现。
----------------------------------------------
武汉天气不好
作者:
2022/9/26 12:21:28
11楼:
直接用if手写一下也没多麻烦 三目运算符的可读性还不如硬来if then, 牺牲掉短路运算就完全不值得。
----------------------------------------------
-