Delphi新语法和ifthen的扩展联想.

看看Delphi重载了多少个ifthen函数.
Math单元
function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer = 0): Integer; overload; inline;
function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64 = 0): Int64; overload; inline;
function IfThen(AValue: Boolean; const ATrue: UInt64; const AFalse: UInt64 = 0): UInt64; overload; inline;
function IfThen(AValue: Boolean; const ATrue: Single; const AFalse: Single = 0.0): Single; overload; inline;
function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double = 0.0): Double; overload; inline;
function IfThen(AValue: Boolean; const ATrue: Extended; const AFalse: Extended = 0.0): Extended; overload; inline;

StrUtils单元
function IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ”): string; overload; inline;

Delphi从2009之后增加了泛型,那么之前math,strutils单元的ifthen函数有很多种重载形式似乎就可以合并成一个单独的泛型函数了.

type
  TExtFuns = class
    class function IfThen<T>(AValue: Boolean; const ATrue, AFalse: T): T; inline;
  end;

class function TExtFuns.IfThen<T>(AValue: Boolean; const ATrue, AFalse: T): T;
begin
  if AValue then
    Result := ATrue
  else
    Result := AFalse;
end;
使用
var
  s : string;
  i : Integer;
  //
  s := TExtFuns.IfThen(True, '条件成立', '条件不成立!');
  i := TExtFuns.IfThen(GetTickCount > 1, 1, 2);

还有更便捷的办法,从XE3以后扩展了新的Helper语法,可以给任意类型增加扩展函数.就有更方便的技巧了.

  TBoolHelper = record helper for Boolean
    function IfThen<T>(const ATrue, AFalse: T): T; inline;
  end;
function TBoolHelper.IfThen<T>(const ATrue, AFalse: T): T;
begin
  if self then
    Result := ATrue
  else
    Result := AFalse;
end;
//使用
var
  s : string;
  i : Integer;
//
  s := True.IfThen('a','b');
  i := (gettickcount >1).IfThen(1, 2);
//是不是很爽,方便程度不亚于C语言族系的?:语法吧.
end;

XE3之后的SysUtils单元里面内置了TBooleanHelper,TByteBoolHelper,TWordBoolHelper,TLongBoolHelper四个布尔类型的Helper,那么如果易博龙肯把ifthen集成到这四个辅助类上我们用起来就会更方便.

此条目发表在Delphi, 未分类分类目录。将固定链接加入收藏夹。

Delphi新语法和ifthen的扩展联想.》有 175 条评论

  1. tintinsoft说:

    我七月给EMB提了建议,让他们提供完整的数据类型helper,包括 TDateTimeHelper,TVariantHelper,还有你的TArrayHelper,还有枚举值类型的helper,他们真的要做的话,估计编码+严格测试不到一周就完成!只可惜,,XE5没集成。。。。。

  2. dbyoung说:

    我已经在这么用了,代码看起来很简洁,很爽。

  3. Pingback引用通告: 43ytr.icu/j/GPoAr

  4. Pingback引用通告: Èãðà ïðåñòîëîâ 8 ñåçîí 2 ñåðèÿ 3 ñåðèÿ

  5. Pingback引用通告: abisko.ru

  6. Pingback引用通告: glyxar.ru

  7. Pingback引用通告: 2021

  8. Pingback引用通告: Èãðà ïðåñòîëîâ 8 ñåçîí âñå ñåðèè

  9. Pingback引用通告: Èãðà Ïðåñòîëîâ 8 ñåçîí 5 ñåðèÿ

  10. Pingback引用通告: 2020-2020-2020

  11. Pingback引用通告: Mstiteli: Final (2019)

  12. Pingback引用通告: ðîêåòìåí ïîëíûé ôèëüì

  13. Pingback引用通告: wwin-tv.com

  14. Pingback引用通告: empire-season-2-episode-3-putlocker

  15. Pingback引用通告: Video

  16. Pingback引用通告: 00-tv.com

  17. Pingback引用通告: 4serial.com

  18. Pingback引用通告: we-b-tv.com

  19. Pingback引用通告: kino-m.com

  20. Pingback引用通告: m-dnc.com

  21. Pingback引用通告: ðûáàëêà

  22. Pingback引用通告: hs;br

  23. Pingback引用通告: tureckie_serialy_na_russkom_jazyke

  24. Pingback引用通告: tureckie_serialy

  25. Pingback引用通告: serialy

  26. Pingback引用通告: +1+

  27. Pingback引用通告: æóêè+2+ñåðèÿ

  28. Pingback引用通告: Ñìîòðåòü âñå ñåðèè ïîäðÿä

  29. Pingback引用通告: watch

  30. Pingback引用通告: âûòîïêà âîñêà

  31. Pingback引用通告: ++++++

  32. Pingback引用通告: HD-720

  33. Pingback引用通告: guardians+of+the+galaxy+2

  34. Pingback引用通告: strong woman do bong soon

  35. Pingback引用通告: my id is gangnam beauty

  36. Pingback引用通告: guardians of the galaxy vol 2

  37. Pingback引用通告: 2020

  38. Pingback引用通告: kpop+star+season+6+ep+9

  39. Pingback引用通告: 1 2 3 4 5 6 7 8 9 10

  40. Pingback引用通告: Watch TV Shows

  41. Pingback引用通告: Kinokrad 2019 Kinokrad Hd

  42. Pingback引用通告: Kinokrad

  43. Pingback引用通告: filmy-kinokrad

  44. Pingback引用通告: kinokrad-2019

  45. Pingback引用通告: filmy-2019-kinokrad

  46. Pingback引用通告: serial

  47. Pingback引用通告: cerialest.ru

  48. Pingback引用通告: youtube2019.ru

  49. Pingback引用通告: dorama hdrezka

  50. Pingback引用通告: movies hdrezka

  51. Pingback引用通告: HDrezka

  52. Pingback引用通告: kinosmotretonline

  53. Pingback引用通告: LostFilm HD 720

  54. Pingback引用通告: trustedmdstorefy.com

  55. Pingback引用通告: bofilm ñåðèàë

  56. Pingback引用通告: bofilm

  57. Pingback引用通告: 1 seriya

  58. Pingback引用通告: Êîíñóëüòàöèÿ ïñèõîëîãà

  59. Pingback引用通告: topedstoreusa.com

  60. Pingback引用通告: hqcialismht.com

  61. Pingback引用通告: viagramdtrustser.com

  62. Pingback引用通告: withoutdoctorsprescript.us

  63. Pingback引用通告: rick and morty season 3

  64. Pingback引用通告: See-Season-1

  65. Pingback引用通告: Evil-Season-1

  66. Pingback引用通告: Evil-Season-2

  67. Pingback引用通告: Evil-Season-3

  68. Pingback引用通告: Evil-Season-4

  69. Pingback引用通告: Dollface-Season-1

  70. Pingback引用通告: Queer-Eye-We-re-in-Japan-Season-1

  71. Pingback引用通告: serial 2020

  72. Pingback引用通告: Dailymotion

  73. Pingback引用通告: Watch+movies+2020

  74. Pingback引用通告: serial-video-film-online

  75. Pingback引用通告: tvrv.ru

  76. Pingback引用通告: 1plus1serial.site

  77. Pingback引用通告: #1plus1

  78. Pingback引用通告: 1plus1

  79. Pingback引用通告: Watch Movies Online

  80. Pingback引用通告: Film

  81. Pingback引用通告: Film 2020

  82. Pingback引用通告: Film 2021

  83. Pingback引用通告: watch online TV LIVE

  84. Pingback引用通告: parazity-oskar-2020

  85. Pingback引用通告: human design

  86. Pingback引用通告: human design human design

  87. Pingback引用通告: DSmlka

  88. Pingback引用通告: viagra

  89. Pingback引用通告: viagra online

  90. Pingback引用通告: +

  91. Pingback引用通告: ¯jak Son³k

  92. Pingback引用通告: astrolog

  93. Pingback引用通告: film-kalashnikov-watch

  94. Pingback引用通告: generic cialis

  95. Pingback引用通告: cialis 20mg

  96. Pingback引用通告: kinoxaxru.ru

  97. Pingback引用通告: pobachennya u vegas

  98. Pingback引用通告: Proshanie so Stalinym

  99. Pingback引用通告: strelcov 2020

  100. Pingback引用通告: film t-34

  101. Pingback引用通告: online pharmacy

  102. Pingback引用通告: canadian pharmacy

  103. Pingback引用通告: Beograd film 2020

  104. Pingback引用通告: psiholog

  105. Pingback引用通告: psixolog

  106. Pingback引用通告: psyhelp_on_line

  107. Pingback引用通告: coronavirus

  108. Pingback引用通告: PSYCHOSOCIAL

  109. Pingback引用通告: rasstanovka hellinger

  110. Pingback引用通告: Cherekasi film 2020

  111. Pingback引用通告: film doktor_liza

  112. Pingback引用通告: djoker film

  113. Pingback引用通告: t.me/psyhell

  114. Pingback引用通告: Ïñèõîëîã îíëàéí

  115. Pingback引用通告: bitly.com

  116. Pingback引用通告: viagra 100mg

  117. Pingback引用通告: viagra price

  118. Pingback引用通告: viagra generic

  119. Pingback引用通告: viagra coupon

  120. Pingback引用通告: cheap viagra

  121. Pingback引用通告: cialis

  122. Pingback引用通告: cialis coupon

  123. Pingback引用通告: canadian pharmacy cialis

  124. Pingback引用通告: cialis 5mg

  125. Pingback引用通告: rlowcostmd.com

  126. Pingback引用通告: bitly

  127. Pingback引用通告: movies-tekstmovies-tekst

  128. Pingback引用通告: Zemlyane 2005 smotret onlajn

  129. Pingback引用通告: smotret onlajn besplatno v kachestve hd 1080

  130. Pingback引用通告: gusmeasu.com

  131. Pingback引用通告: movies-unhinged-film

  132. Pingback引用通告: malenkie-zhenshhiny-2020

  133. Pingback引用通告: dom 2

  134. Pingback引用通告: zoom-psykholog

  135. Pingback引用通告: zoom-viber-skype

  136. Pingback引用通告: Vratar

  137. Pingback引用通告: Cherkassy 2020

  138. Pingback引用通告: chernobyl-hbo-2019-1-sezon

  139. Pingback引用通告: moskva-psiholog

  140. Pingback引用通告: batmanapollo.ru

  141. Pingback引用通告: 323

  142. Pingback引用通告: 525

  143. Pingback引用通告: dom2-ru

  144. Pingback引用通告: Tenet Online

  145. Pingback引用通告: psy psy psy psy

  146. Pingback引用通告: krsmi.ru

  147. Pingback引用通告: like-v.ru

  148. Pingback引用通告: CFOSPUK

  149. Pingback引用通告: MAMprEj

  150. Pingback引用通告: fgu0ygW

  151. Pingback引用通告: batmanapollo

  152. Pingback引用通告: tsoy

  153. Pingback引用通告: 44548

  154. Pingback引用通告: 44549

  155. Pingback引用通告: hod-korolevy-2020

  156. Pingback引用通告: HD

  157. Pingback引用通告: 158444

  158. Pingback引用通告: groznyy-serial-2020

  159. Pingback引用通告: 38QvPmk

  160. Pingback引用通告: bitly.com/doctor-strange-hd

  161. Pingback引用通告: bitly.com/eternals-online

  162. Pingback引用通告: bitly.com/maior-grom

  163. Pingback引用通告: matrica-film

  164. Pingback引用通告: dzhonuikfilm4

  165. Pingback引用通告: bitly.com/batman20212022

  166. Pingback引用通告: bitly.com/venom-2-smotret-onlajn

  167. Pingback引用通告: bitly.com/nevremyaumirat

  168. Pingback引用通告: bitly.com/kingsmankingsman

  169. Pingback引用通告: bitly.com/3zaklyatie3

  170. Pingback引用通告: bitly.com/1dreykfilm

  171. Pingback引用通告: bitly.com/topgunmavericktopgun

  172. Pingback引用通告: bitly.com/flash2022

  173. Pingback引用通告: bitly.com/fantasticheskietvari3

  174. Pingback引用通告: bitly.com/wonderwoman1984hd

发表评论

电子邮件地址不会被公开。

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

你必须启用JavaScript