在Delphi中有条件运算符是否存在? [英] Is there, or is there ever going to be, a conditional operator in Delphi?

查看:171
本文介绍了在Delphi中有条件运算符是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把我的手从德尔福手中停了好久,我猜:在过去的几年里,我已经用Java和PHP自己做了很多工作。现在,当我回到做一些Delphi工作时,我意识到我真的很想念Java和PHP支持的条件运算符。



在多少个地方你在Delphi程序中找到这样的行?

  var s:string; 
begin
...<这里的字符串结果被操纵> ...

如果combo.Text =''然后
s:='null'
else
s:= QuotedStr(combo.Text);

result:= result + s;
结束

其中一个简单的

  result:= result +(combo.text ='')?'null':quotedStr(combo.text); 

就够了。我喜欢这个是,它不仅缩短了代码,这样我也避免声明一些帮助 s:string 变量。



为什么条件运算符不是Delphi的一部分,而且他们是否得到支持?我注意到2009年版Delphi(泛型)有不少语言扩展,所以为什么不添加这个功能?

解决方案

p>这样一个操作符不是当前Delphi版本的一部分,因为它不是以前版本的一部分,并且需求不足以证明添加它的成本。 (您会发现该解释适用于您希望在 的产品中使用的功能的 。)



Delphi在Math和StrUtils单元中提供了一组 IfThen 函数,但它们具有评估它们的值参数的不幸属性,因此这样的代码将失败: Foo:= IfThen(Obj = nil,'< none>',Obj.Name); p>

  

为了真正做到正确,编译器需要帮助。在Delphi社区中,我感到一般不喜欢使用问号和冒号的C风格语法。我已经看到使用这样的语法的提案:

  Foo:=如果Obj = nil then 
'< ; none>'
else
Obj.Name;

使条件运算符如此吸引人的一部分是他们让你编写简洁的代码,但Delphi的风格写出所有内容使得上述不合适,即使全部放在一行。



它不是真的需要以运算符的形式。 Delphi Prism提供了一个编译器魔术功能 Iif 只评估其两个值参数之一:

  Foo:= Iif(Obj = nil,'< none>',Obj.Name); 

你问为什么这样的功能不会被添加到添加的所有其他语言功能在德尔福2009年。我认为这是你的理由。还有很多其他的语言变化,已经需要微妙的处理;开发商不需要更多的负担。特征不是免费的。



你问Delphi是否会有这样的功能。我不是Embarcadero的计划会议的秘密,我不得不把我的水晶球送去修理,所以我不能肯定地说,但我预测,如果它有将具有这样的功能它将以Delphi Prism的 Iif 函数的形式出现。这个想法会在质量中心的讨论结尾附近显示,以及有人反对,作为一个新的保留字,它将破坏与已经定义具有相同名称的函数的其他人的代码的向后兼容性。但是,这不是一个有效的对象,因为它不需要是保留字。它可以是一个标识符,就像 Writeln 退出一样,它可以有资格重新定义在其他单位甚至尽管系统单元中的一个被特别处理。


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?

解决方案

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:

Foo := IfThen(Obj = nil, '<none>', Obj.Name);

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:

Foo := if Obj = nil then
         '<none>'
       else
         Obj.Name;

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:

Foo := Iif(Obj = nil, '<none>', Obj.Name);

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 like Writeln and Exit, it can be eligible to be redefined in other units even though the one from the System unit is treated specially.

这篇关于在Delphi中有条件运算符是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆