Delphi - 相当于 C# 的三元运算符?

发布于 2024-10-21 03:12:19 字数 375 浏览 9 评论 0原文

可能的重复:
是否有,或者Delphi 中是否存在条件运算符?

我知道 Delphi 没有像 C# 中那样的三元运算符。 ie ?:

那么如何最好地表示这个函数调用呢?最干净的方法是什么?

如果有任何代码可以用来代替编写单独的函数,那就太好了?如果不是,那么最有效和/或最干净的代码表示是什么?

Possible Duplicate:
Is there, or is there ever going to be, a conditional operator in Delphi?

I understand Delphi doesnt have the ternary operator as in C#.
i.e. ?:

So how best to represent this function call? Whats the cleanest method out there?

Would be very nice if there is any code out there that can be used INSTEAD of writing a separate function? If not, whats the most efficient and/or cleanest code representation of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘你却要生生世世 2024-10-28 03:12:19

当然,您可以

IfThen(SomeBooleanExpression, IfTrueReturnValue, IfFalseReturnValue)

在返回值为数字(使用 Math)或字符串(使用 StrUtils)的情况下使用。但请注意,这将在所有情况下计算两个参数 - 没有惰性计算,因此它不如 C# 中的 ?: 运算符高效,后者仅计算正确的操作数。

所以你不能做

y := IfThen(x <> 0, 1/x, 0)

最好的事情就是坚持平凡

if x <> 0 then y := 1/x else y := 0;

Of course you can use

IfThen(SomeBooleanExpression, IfTrueReturnValue, IfFalseReturnValue)

where the return values are numeric (uses Math) or string (uses StrUtils). But notice that this will evaluate both arguments in all cases -- there is no lazy evaluation, so it is not as efficient as the ?: operator in C#, where only the right operand is evaluated.

So you cannot do

y := IfThen(x <> 0, 1/x, 0)

The best thing is to stick with an ordinary

if x <> 0 then y := 1/x else y := 0;
苯莒 2024-10-28 03:12:19

最接近三元运算符的是:

if (condition) then <statement> else <statement>;

据我所知,Delphi 中没有三元运算符。

The closest you can get to a ternary operator is:

if (condition) then <statement> else <statement>;

As far as I remember, there is no ternary operator in Delphi.

不顾 2024-10-28 03:12:19

试试 Jedi 中的 Iff:

function Iff(const Condition: Boolean; const TruePart: string; const FalsePart: string): string; overload;
function Iff(const Condition: Boolean; const TruePart: Char; const FalsePart: Char): Char; overload;
function Iff(const Condition: Boolean; const TruePart: Byte; const FalsePart: Byte): Byte; overload;
function Iff(const Condition: Boolean; const TruePart: Integer; const FalsePart: Integer): Integer; overload;
function Iff(const Condition: Boolean; const TruePart: Cardinal; const FalsePart: Cardinal): Cardinal; overload;
function Iff(const Condition: Boolean; const TruePart: Float; const FalsePart: Float): Float; overload;
function Iff(const Condition: Boolean; const TruePart: Boolean; const FalsePart: Boolean): Boolean; overload;
function Iff(const Condition: Boolean; const TruePart: Pointer; const FalsePart: Pointer): Pointer; overload;
function Iff(const Condition: Boolean; const TruePart: Int64; const FalsePart: Int64): Int64; overload;
function Iff(const Condition: Boolean; const TruePart: Variant; const FalsePart: Variant): Variant; overload;

Try the Iff from Jedi:

function Iff(const Condition: Boolean; const TruePart: string; const FalsePart: string): string; overload;
function Iff(const Condition: Boolean; const TruePart: Char; const FalsePart: Char): Char; overload;
function Iff(const Condition: Boolean; const TruePart: Byte; const FalsePart: Byte): Byte; overload;
function Iff(const Condition: Boolean; const TruePart: Integer; const FalsePart: Integer): Integer; overload;
function Iff(const Condition: Boolean; const TruePart: Cardinal; const FalsePart: Cardinal): Cardinal; overload;
function Iff(const Condition: Boolean; const TruePart: Float; const FalsePart: Float): Float; overload;
function Iff(const Condition: Boolean; const TruePart: Boolean; const FalsePart: Boolean): Boolean; overload;
function Iff(const Condition: Boolean; const TruePart: Pointer; const FalsePart: Pointer): Pointer; overload;
function Iff(const Condition: Boolean; const TruePart: Int64; const FalsePart: Int64): Int64; overload;
function Iff(const Condition: Boolean; const TruePart: Variant; const FalsePart: Variant): Variant; overload;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文