Ternary Operators (Request)
Hello, can we please have ternary operators added to the AL language?
For example, being able to write this (this is just a quick example, not something I am working on):
ShipToAddr.Get(Customer."Customer No.", Customer."Ship-to Code" = '' ? DefaultShipToAddr.Code : Customer."Ship-to Code");
instead of
if Customer."Ship-to Code" = '' then ShipToAddr.Get(Customer."Customer No.", DefaultShipToAddr.Code) else ShipToAddr.Get(Customer."Customer No.", Customer."Ship-to Code");
would be cleaner and much less cumbersome.
Thank you!
I think you mean ternary operator :-) but yes, please! this would be really nice, and I find myself wanting it very frequently.
I think you mean ternary operator :-) but yes, please! this would be really nice, and I find myself wanting it very frequently.
Quite right! Thanks for the correction
Is this enhancement currently planned? It would be a great enhancement to the AL language, because it would lead to cleaner code by introducing another possibility for conditional statements instead of the "bulky" if ... then ... else.
Thanks and best regards.
One wrinkle here is that ternary operators in other languages short-circuit, but our venerable if/else don't. If a ternary was added to AL, people who are familiar with that operator from other languages might write code with unexpected results if they (consciously or not) assumed that ?: would perform short-circuiting.
My vote? We add andAlso and orElse keywords that do short-circuit. Then - perhaps - we could add ?: and document that it behaves like said new andAlso/orElse, i.e. always short-circuit - to avoid saddling it with the same burden as our unadorned and and or. But... I am wary of whether our dev community's collective brain can really handle short-circuiting ;-)
Since the dev community managed to transfer to a more modern IDE and AL in general, I think chances are quite good that we should be able to handle that ;)
This belongs at aka.ms/bcideas. This is currently tracked by at least one idea over there: https://experience.dynamics.com/ideas/idea/?ideaid=d62c7a8d-7f56-ec11-a3ee-0003ff45ac6d