Delphi 中是否存在或者将来是否存在条件运算符?
我猜我已经太长时间没有接触德尔福了;在过去的几年里,我一直忙于 Java 和 PHP。现在,当我重新开始做一些 Delphi 工作时,我意识到我真的很怀念 Java 和 PHP 都支持的条件运算符。
在您的 Delphi 程序中,您会在多少地方找到这样的行?
var s : string;
begin
...<here the string result is manipulated>...
if combo.Text='' then
s := 'null'
else
s := QuotedStr(combo.Text);
result := result + s;
end;
一个简单的
result := result + (combo.text='')?'null':quotedStr(combo.text);
就足够了。我喜欢这个的原因是它不仅缩短了代码,这样我还可以避免声明一些辅助 s:string
变量。
为什么条件运算符不是 Delphi 的一部分以及 - 它们会受到支持吗?我注意到 2009 版本的 Delphi(泛型)有相当多的语言扩展,那么为什么不添加这个功能呢?
I kept my hands off Delphi for too long, I guess; busied myself with Java and PHP a lot over the last couple of years. Now, when I got back to doing a little Delphi job, I realised I really miss the conditional operator which is supported by both Java and PHP.
On how many places would you find lines like these in your Delphi programs?
var s : string;
begin
...<here the string result is manipulated>...
if combo.Text='' then
s := 'null'
else
s := QuotedStr(combo.Text);
result := result + s;
end;
where a simple
result := result + (combo.text='')?'null':quotedStr(combo.text);
would suffice. What I like about this is that it not only shortens the code, this way I also avoid declaring some helper s:string
variable.
Why are conditional operators not part of Delphi and - are they ever going to be supported? I noticed there were quite a few language extensions made for the 2009 version of Delphi (generics), so why not add this feature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这样的操作符不是当前 Delphi 版本的一部分,因为它不是以前版本的一部分,并且需求不足以证明添加它的成本是合理的。 (您会发现该解释适用于很多您希望在很多产品中拥有的功能。)
Delphi 提供了一组
IfThen
函数在 Math 和 StrUtils 单元中,但它们有一个不幸的特性,即评估它们的值参数,因此这样的代码将会失败:要真正做到正确,需要编译器的帮助。在 Delphi 社区中,我感觉到普遍不喜欢使用问号和冒号的 C 风格语法。我见过使用这样的语法的提案:
条件运算符如此有吸引力的部分原因是它们可以让您编写简洁的代码,但是 Delphi 将所有内容都写出来的风格使得上述内容没有吸引力,即使将所有内容都放在一行上。
它实际上并不需要采用运算符的形式。 Delphi Prism 提供了一个编译器魔法函数
Iif
,仅评估其两个值参数之一:您问为什么这样的功能不会与 Delphi 2009 中添加的所有其他语言功能一起添加。我认为这就是您的原因。还有许多其他语言变化正在进行,需要精心处理;开发人员不需要承担更多的负担。功能不是免费的。
您问Delphi 是否会有这样的功能。我不了解内河码头的计划会议,而且我不得不将水晶球送去维修,所以我不能肯定地说,但我预测如果它会有有这样的功能,它将以 Delphi Prism 的
Iif
函数的形式出现。这个想法出现在 Quality Central 的讨论快结束时,并且有人反对说,作为一个新的保留字,它会破坏与已经定义同名函数的其他人的代码的向后兼容性。但这不是一个有效的对象,因为它不需要是保留字。它可以是一个标识符,就像Writeln
和Exit
一样,它可以在其他单元中重新定义,即使来自 System 单元的标识符被特殊对待。Such an operator isn't part of the current Delphi version because it wasn't part of the previous version, and demand wasn't great enough to justify the cost of adding it. (You'll find that explanation applies to lots of features you wish you had in lots of products.)
Delphi provides a set of
IfThen
functions in the Math and StrUtils units, but they have the unfortunate property of evaluating both their value parameters, so code like this will fail:To really do it right, there needs to be help from the compiler. Within the Delphi community, I sense a general dislike of the C-style syntax using a question mark and a colon. I've seen proposals that would use syntax like this:
Part of what makes conditional operators so attractive is that they let you write concise code, but Delphi's style of writing everything out makes the above unappealing, even if put all on one line.
It doesn't really need to be in the form of an operator. Delphi Prism provides a compiler-magic function
Iif
that only evaluates one of its two value parameters:You asked why a feature like this wouldn't have been added along with all the other language features added in Delphi 2009. I think that's your reason. There were plenty of other language changes going on that already required delicate handling; the developers didn't need to be burdened with even more. Features aren't free.
You asked whether Delphi will ever have such a feature. I'm not privy to Embarcadero's planning meetings, and I had to send my crystal ball away for repairs, so I can't say for certain, but I predict that if it ever would have such a feature, it would come in the form of Delphi Prism's
Iif
function. That idea shows up near the end of the discussion in Quality Central, and an objection is made that, as a new reserved word, it would break backward compatibility with other people's code that already defines a function with the same name. That's not a valid object, though, because it wouldn't need to be a reserved word. It could be an identifier, and just likeWriteln
andExit
, it can be eligible to be redefined in other units even though the one from the System unit is treated specially.好的。今天的 WTF 代码 :)
如何获得主要类似于三元/条件函数的东西。
是的,它或多或少没什么用,但它表明它是可以做到的。
Ok. WTF code of the day :)
How to get something that mostly acts like a ternary/conditional function.
Yeah, it's more or less useless, but it shows that it can be done.
有一个关于此的 QC 报告 (8451),其中进行了合理的讨论。
2004 年 6 月提出,Borland/CodeGear/Embarcadero 似乎没有任何回应。
There's a QC report on this (8451) which has a reasonable discussion.
Raised June 2004, and there doesn't appear to be any response from Borland/CodeGear/Embarcadero.
重载的 IFTHEN 函数有许多可用的简单类型句柄。
StrUtils.IfThen
(String
)Math.IfThen
(Integer
)Math.IfThen
(Int64
)Math.IfThen
(Double
)(也适用于TDateTime
)此模型如下所示在 Andreas 评论的示例中,但对于简单类型来说,这是非常合理的。如果遵循 Delphi/Pascal 方法约定,而不是屈服于使用尽可能少的字符的 C 方式。
就我个人而言,我不希望看到 Delphi 中引入条件运算符(即
?:
),因为与 C 及其衍生语言相比,我更喜欢 Delphi/Pascal 的可读性。我更愿意看到更多创新的 Delphi 类型解决方案来解决这样的问题,而不是实现更多的 C 主义。There are a number of available simple type handles on the overloaded IFTHEN function.
StrUtils.IfThen
(String
)Math.IfThen
(Integer
)Math.IfThen
(Int64
)Math.IfThen
(Double
) (works forTDateTime
as well)This model falls down as shown in the example that Andreas commented on, but for simple types this is more than reasonable. If follows the Delphi/Pascal convention of methods rather than succumbing to the C way of using the least amount of characters as possible.
Personally I would rather not see a conditional operator (i.e.
?:
) introduced in Delphi as I prefer the readability of Delphi/Pascal over C and it derivative languages. I would prefer to see more innovative Delphi type solutions to something like this than to implement more C-isms.Delphi 中没有条件运算符,我严重怀疑是否会有条件运算符,但你可能永远不知道。您可以随时在 Embarcadero 提出请求。
另一种方法是定义 Iff 函数:
其中 XXX 是所需的类型。
使用 as:
不实现条件运算符有多种原因。其中之一是可读性。 Pascal(还有 Delphi)比 C 语法语言更注重可读性,C 语法语言更注重字符能力(每个字符尽可能多的信息)。条件运算符功能强大,但(根据某些人的说法)不可读。但如果你看看 Delphi 中的(可怕的)with 语句......(无需多说)。
另一个原因是不需要条件运算符。这是真的。但还有更多不需要的内容仍在实施中。
最后,这只是一个品味问题。
但是,如果您只想评估一个参数,则始终可以使用以下内容,这违反了字符能力概念的可读性:
[overdesignmode]
[/overdesignmode]
There is no conditional operator in Delphi, and I seriously doubt if there will ever be one but you may never know. You can always issue a request at Embarcadero.
An alternative is to define the Iff function:
Where XXX is the desirec type.
Use as:
There are several reasons why not to implement the conditional operator. One of these is readability. Pascal (and also Delphi) is more centered on Readability than the C Syntax languages which are more centered on character power (as much information per character as possible). The conditional operator is powerful but (according to some) not readable. But if you look at the (dreaded) with statement in Delphi... (no need to say more).
Another reason is that the conditional operator is not required. Which is true. But there is more not required that is still implemented.
In the end it's just a matter of taste.
But if you want just one argument to be evaluated, you can always use the folowing, which violates both the readability as the character power concept:
[overdesignmode]
[/overdesignmode]
我更希望他们实现惰性求值,它会更强大并且可以在不同的场景中使用。请参阅以下链接的详细信息
http://www.digitalmars.com/d/2.0 /lazy-evaluation.html
干杯
I would prefer them to to implement lazy evaluation and it will be more powerful and can be used in different scenario. See detail as below link
http://www.digitalmars.com/d/2.0/lazy-evaluation.html
Cheers
另一种选择是使用泛型:
这是非常可读的:
注意:正如 Alan Bryant(于 6/21/2004 7:26:21 AM)在 QR 8451,这将始终评估所有 3 个参数 - 因此请注意,它不是真正的三元运算符。
Another option is to use generics:
This is quite readable:
note: as pointed out by Alan Bryant (at 6/21/2004 7:26:21 AM) in QR 8451, this will always evaluate all 3 arguments - so be aware that it's not a true ternary operator.
实际上,对于字符串,您可以使用:StrUtils.IfThen函数:
查看delphi帮助wiki:http://docwiki.embarcadero.com/VCL/en/StrUtils.IfThen
它完全符合您的需要。
Actually for strings you can use the: StrUtils.IfThen function:
Look in the delphi help wiki: http://docwiki.embarcadero.com/VCL/en/StrUtils.IfThen
It does exactly what you need.
更好的是支持多种数据类型和结果的重载 IIF(内联 if)。
Better yet is an overloaded IIF (inline if) that supports multiple datatypes and results.
Jedi 代码库 ( JCL ) 使用一组名为 Iff() 的函数实现了三元运算符。请参阅此处的文档:
http://wiki.delphi-jedi。 org/wiki/JCL_Help:Iff@Boolean@Boolean@Boolean
要下载 JCL,您可以访问此站点:
http://sourceforge.net/projects/jcl/
Jedi Code Library ( JCL ) has implemented the ternary operaror with a set of functions named Iff(). See here for documentation:
http://wiki.delphi-jedi.org/wiki/JCL_Help:Iff@Boolean@Boolean@Boolean
To download JCL you can visit this site:
http://sourceforge.net/projects/jcl/
当天的 WTF 代码 Nr. 2:
Variant 数据类型的一种可能的修改是:
WTF code of the day Nr. 2:
One possible modification for Variant data type is:
如果有帮助的话,相对于 Math.IfThen 函数的最大好处是它使用“内联”(假设编译器支持它)。
带有 TFunc 或 TFunc的版本使用时会有点难看,因为 Delphi 的匿名方法没有 Lambda 表达式(尽管我发现了一些使用 Variant 表达式的实验)
In case it helps, most benefit over Math.IfThen functions is that it uses "inline" (assuming compiler honors it).
The version with TFunc or TFunc<T,T> would be a bit ugly when using since Delphi doesn't have Lambda expressions (though I found some experiment with Variant expressions) for its anonymous methods