Function Overload in AL. When?
Views (805)
Introduction.
In the end of 2017 overload method is able in AL:
This means that we can declare more than a function with the same name, but with diferents parameters in their firms.
The function to execute depends to the parameters the caller is using, the compiler uses these parameters to select the right declaration.
When we see this feature, our first reaction could be asking what the real value of this feature is. I notice there was already an old discussion in other programming languages about it, between critics and subscribers of method overload.
The value of a name.
The first argument in support of overload is maintain name consistency:
local procedure MinValueBetwenTwo(Fecha1: Date; Fecha2: Date): Date
begin
if Fecha1 < Fecha2 then
exit(Fecha1);
exit(Fecha2);
end;
local procedure MinValueBetwenTwo(Decimal1: Decimal; Decimal2: Decimal): Decimal
begin
if Decimal1 < Decimal2 then
exit(Decimal1);
exit(Decimal2);
end;
We can preserve the name “MinvalueBetweenTwo” to process date and decimal. Supporters argue the advantage to preserve an unique name, and not to force to remember two distinct names. I don´t see a great advantage with intellisense, that able use a common start of the word as we type it:
I´d rather use this option, two different names with the same starting characters.
A very important advertising: be sure functions with the same name do the same!!! Other way consistency become chaos.
Backward compatibility usage.
We begin with a function called outside by other code:
procedure CustomerCommentCount(Customer: Record Customer): Integer
var
Comment: Record "Comment Line";
begin
with Comment do begin
SetRange("Table Name","Table Name"::Customer);
SetRange("No.",Customer."No.");
exit(Count());
end;
end;
We notice is simpler to call it only by customer number, instead customer record in a later revision:
procedure CustomerCommentCount(CustomerNo: Code[20]): Integer
var
Comment: Record "Comment Line";
begin
with Comment do begin
SetRange("Table Name","Table Name"::Customer);
SetRange("No.",CustomerNo);
exit(Count());
end;
end;
We can ensure compatibility with existing third party calls creating an overload function with old record parameter, but avoiding duplication calling the new function this way:
procedure CustomerCommentCount(Customer: Record Customer): Integer
begin
exit(CustomerCommentCount(Customer."No."));
end;
Conclusion.
Overload method have been discussing from long time ago in other programming groups. Must be careful using overload, could be good to maintain names but only if functions do the same. Can be very useful to maintain backward compatibility when we change function parameters firm.

Like
Report
*This post is locked for comments