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的扩展联想.》有 115 条评论

  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引用通告: generic viagra

  114. Pingback引用通告: buy viagra online

admin进行回复 取消回复

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

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

你必须启用JavaScript