Jump to content
EugeneK

A Conditional Ternary Operator for the Delphi

Recommended Posts

First impression: nice to have this operator.

Second impression: supporting code running in for FPC and Delphi becomes harder and harder.

 

I would prefer to have one of items below in Delphi and FPC:

  1. pure generic functions with syntax function XXX<T>(paramas...): result_type  . Having such function covers case above and many other cases.
  2. operator if overloading.

Share this post


Link to post

Wow, I hated the ternary operator in every language I encountered so far. But this one with pascal keywords seems almost bearable.

But, I'm sure it will bring all sorts of new anti-patterns to Delphi. And I don't think a language saddled with 'with' needs any more anti-patterns.

  • Like 3

Share this post


Link to post
1 hour ago, EugeneK said:
Quote

This has been one of the most requested language enhancements by customers for quite some time

Yeah, right 🤦‍♂️

 

More syntactic sugar, the compiler codegen is still a joke, and we still haven't got SIMD intrinsics.

 

The only time I've ever wished for a ternary operator was when porting C code and being in a rush. But I wouldn't mind all this fluff (since I don't have to use it) - if they would also throw an occasional bone to those of us that need low level performance.

 

57 minutes ago, @AT said:

Second impression: supporting code running in for FPC and Delphi becomes harder and harder.

Yes, but you don't have to use it.

  • Like 3

Share this post


Link to post
30 minutes ago, Anders Melander said:

Yeah, right 🤦‍♂️

 

It is true at least based on votes in old quality.embarcadero.com portal.

Share this post


Link to post
3 minutes ago, EugeneK said:

It is true at least based on votes in old quality.embarcadero.com portal.

Perhaps, but I seriously doubt that it's a representative metric with regard to the needs of the current Delphi users.

 

In all of the Delphi shops that I've ever worked in, I think I was the only one who ever posted something there and even I gave up on that in the end.

Share this post


Link to post
5 hours ago, Remy Lebeau said:

Actually, a patch was already created for FreePascal

Good news. Thank you Remy for keeping us informed!

 

However, Next improvements can bring more value in the Delphi and FreePascal languages:

  1. As I pointed earlier - pure generic function declaration can help much. 
  2. generic constrains by type sets like it implemented in the GoLang
  3. generate smarter code for generic methods. Now the code duplicates for all types.

Implementing 1 and 2 allows to implement regular functions like:

iif<T>(Condition: boolean; A, B: T): T; overload; inline; begin if Condition then Result:=A else Result:=B; end;
max<T: number>(A,B: T); overload; inline; begin if A=>B then Result:=A else Result:=B; end;
min<T: enum>(A,B: T); inline; begin if A<=B then Result:=A else Result:=B; end;

etc.

P.S.

number, enum, etc. are could be group sets which union group of compatible types.

Edited by @AT
  • Like 1

Share this post


Link to post

I wonder whether there will be a code formatter that supports this. Given that they deprecated the existing one since Delphi 11, I somehow doubt it.

  • Like 2
  • Haha 1

Share this post


Link to post
4 hours ago, Remy Lebeau said:

Actually, a patch was already created for FreePascal to add this very same conditional operator (FPC already had an IfThen() intrinsic);

 

https://forum.lazarus.freepascal.org/index.php/topic,71398.msg556926.html#msg556926

I don't think so, if you read the full topic you'll see that there is no patch on FPC and they are working on (I don't know how much).

May be after the public release of Rad Studio 13 whit all notes they will speed up the production of a patch.

Edited by DelphiUdIT

Share this post


Link to post

Make a prevision on what they will produce is only a fantasy. I think, 'cause the next is a major release, that will only be the beginning of a series of new features that will be proposed to us.

But, ...... if some MVP were authorized to release some other substantial news, even if not seasoned with technical details ....

Edited by DelphiUdIT

Share this post


Link to post

On this atleast syuntax is readable

X := if Left < 100 then 22 else 45;

For example C-syntax is not what I really can say that I like.


int result = (x > 5) ? 10 : 20;

even worse if writte like this


int result=(x>5)?10:20;

Syntax highlighting saves a bit for sure but still easy to miss those... One character operator thingies...

 

-Tee-

  • Like 1

Share this post


Link to post
12 hours ago, EugeneK said:

It is not clear from blog post are both expressions evaluated every time or based on condition? I think is most important thing about this feature.

In the example thay gave, the equivalent expression is a typical IF, so we can deduce that the new expression also behaves in this way: the part not affected by the condition will not be evaluated.
Of course, only they or the release can confirm this.

Edited by DelphiUdIT
  • Like 1

Share this post


Link to post

Great! This is a very long awaited addition for me.

I hope they also add a case statement for string types

  • Like 3

Share this post


Link to post
2 hours ago, DelphiUdIT said:

if some MVP were authorized to release some other substantial news, even if not seasoned with technical details ....

The beta is under NDA, we can't talk except when we are authorized... or after an official post like for the ternary operator.

 

Many new (and updated) things are in the Ganymede beta(s), not sure all will be released for 13.0 Florence. Stay tuned. 😉

  • Thanks 1

Share this post


Link to post
27 minutes ago, DelphiUdIT said:

In the example thay gave, the equivalent expression is a typical IF, so we can deduce that the new expression also behaves in this way: the part not affected by the condition will not be evaluated.
Of course, only they or the release can confirm this.

 

The Delphi ternary operator will be a real ternary operator. It's different from ifthen() functions.

Share this post


Link to post
17 minutes ago, Kryvich said:

Great! This is a very long awaited addition for me.

I hope they also add a case statement for string types

You can look at examples posted, they use strings in ShowMessage .... 

Edited by DelphiUdIT
Answer not related ...

Share this post


Link to post
2 hours ago, DelphiUdIT said:

if some MVP were authorized to release some other substantial news, even if not seasoned with technical details

If you are a current subscriber to Delphi, you should have received an invitation to Beta test....

Share this post


Link to post
1 minute ago, DelphiUdIT said:

You can look at examples posted, they use strings in ShowMessage ....

yes, but it's different from a "case" with strings like

Quote

 

case s of

'hello':dosomething;

else

dootherthig;

end;

 

 

Share this post


Link to post
1 minute ago, Patrick PREMARTIN said:

yes, but it's different from a "case" with strings like

 

 

12 minutes ago, Kryvich said:

Great! This is a very long awaited addition for me.

I hope they also add a case statement for string types 

Sorry, I understood wrong the question. My fault.

It's better thta I wait some more details before speaking ...

Share this post


Link to post
9 hours ago, @AT said:

 

However, Next improvements can bring more value in the Delphi and FreePascal languages:

  1. As I pointed earlier - pure generic function declaration can help much. 
  2. generic constrains by type sets like it implemented in the GoLang
  3. generate smarter code for generic methods. Now the code duplicates for all types.

Implementing 1 and 2 allows to implement regular functions like:


iif<T>(Condition: boolean; A, B: T): T; overload; inline; begin if Condition then Result:=A else Result:=B; end;
max<T: number>(A,B: T); overload; inline; begin if A=>B then Result:=A else Result:=B; end;
min<T: enum>(A,B: T); inline; begin if A<=B then Result:=A else Result:=B; end;

 

The difference between a ternary operator and iif<T> as you describe is that both operators are not evaluated with the ternary operator. Only the one satisfying the condition

 

 For example

 

isVisible:=if Assigned(Foo) then Foo.Visible else False

 

which will work vs

 

isVisible:=iif<Boolean>(Assigned(Foo),Foo.Visible,False)

 

which will crash if Foo is not assigned, according to the implementation you describe.

Share this post


Link to post
10 hours ago, DelphiUdIT said:

I don't think so, if you read the full topic you'll see that there is no patch on FPC and they are working on (I don't know how much).

I didn't say it was *submitted* for merge into FPC. I said it was *created*. There is a patch file attached to the forum post that I linked to:

 

"It's roughly 30 lines of code and took 15 minutes to create the attached patch for fpc main"

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×