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.