AL icon indicating copy to clipboard operation
AL copied to clipboard

Ternary Operators (Request)

Open MBK75 opened this issue 5 years ago • 5 comments

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!

MBK75 avatar Jun 11 '20 16:06 MBK75

I think you mean ternary operator :-) but yes, please! this would be really nice, and I find myself wanting it very frequently.

dzzzb avatar Jun 12 '20 10:06 dzzzb

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

MBK75 avatar Jun 14 '20 13:06 MBK75

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.

MichaelSchauer98 avatar Oct 11 '22 11:10 MichaelSchauer98

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 ;-)

dzzzb avatar Oct 11 '22 11:10 dzzzb

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 ;)

MichaelSchauer98 avatar Oct 11 '22 11:10 MichaelSchauer98

Hi, Here is an alternative approach we are using.

TernaryOperator Gist

WhatTheHellisThat avatar Mar 10 '23 09:03 WhatTheHellisThat

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

EmilDamsbo avatar May 23 '23 11:05 EmilDamsbo