AL icon indicating copy to clipboard operation
AL copied to clipboard

Method Overloading missing compile error

Open Avanti05 opened this issue 8 years ago • 9 comments

When using method overloading you don't get a compile error when using record as parameter, but deployment will fail (CS0111). Will it be possible at some point to do overloading on different records e.g.

procedure SomeFunction(SalesInvLine : record "Sales Invoice Line") begin end;

and

procedure SomeFunction(SalesShpmtLine : record "Sales Shipment Line) begin end;

If not then at least this should result in a compile error..

Avanti05 avatar Mar 14 '18 11:03 Avanti05

My last testresult was: Method overloading works in devpreview but not in CU03. VSIX from devpreview is 0.14.xx in cu03 is only 0.12.177720

the 0.12.x will show you the error message: 'A member with name 'foo' is already defined'

DennisReinecke avatar Mar 14 '18 11:03 DennisReinecke

Hi @Avanti05 ! @DennisReinecke is correct. Also please keep in mind that you should use a VSIX that matches your runtime version. If you do not do this you might get into a situation where a feature works in VSCode, but fails on publishing.

atoader avatar Mar 14 '18 12:03 atoader

I'm running a february devpreview with a 0.14.17461 VSIX, so that shouldn't be an issue. If I create an overloaded method with simple type parameters (i.e. integer, text, code etc.), the behavior is as expected. But when I use records as parameter i would expect a compile error, if using records of different types isn't supported.

Avanti05 avatar Mar 14 '18 14:03 Avanti05

You are right, I see the problem now. Thanks for reporting!

atoader avatar Mar 14 '18 14:03 atoader

You're Welcome. Do you know if the fix will be a compile error, or if it will be possible to use different record types as parameters?

Avanti05 avatar Mar 15 '18 07:03 Avanti05

I am still thinking if it will be possible to make this work. For overloads that use types that are distinct in the runtime, we rely on C# for doing overload resolution. For example:

procedure DoStuff(i:Integer)...
procedure DoStuff(j:Decimal)...

will be emitted as

public DoStuff(int i)...
public DoStuff(decimal j)...

it will work perfectly because C# can perform overload resolution and the two methods have different signatures.

Now, for your case:

procedure SomeFunction(SalesInvLine : record "Sales Invoice Line")
begin
end;

procedure SomeFunction(SalesShpmtLine : record "Sales Shipment Line)
begin
end;

we emit (something similar to):

public SomeFunction(INavRecordHandle SalesInvLine )

public SomeFunction(INavRecordHandle SalesShpmtLine)

As you can see, the ID of the record is lost and the C# compiler can't distinguish between the two methods. There are solutions for this, but the ones I've found so far would break BC.

We will discuss this next week and try to find the best compromise.

atoader avatar Mar 16 '18 11:03 atoader

Very interesting issue with a lot of depending on the resolution. Two points:

  1. I am expecting that the submitter knows that you can define the procedure that takes variant as parameter and then resolves internally, what kind of record it is and executes the logic accordingly (a sort of "poor man's overloading").

  2. Obviously, I have a very limited understanding how the emitted code is produced, but just as a wild guess based suggestion - would it be possible to do something like C# does with generics? I.e. public SomeFunction(INavRecordHandle<SalesInvLine> salesInvLine)

Edit: just as an afterthought, a question for the team - are you aiming to avoid creating a specific .NET type for each of the record types?

vytjak avatar Mar 16 '18 12:03 vytjak

@vytjak I'm aware that I can work around this using variant or refordref, but I do think that it would be a nicer solution if we could use overloading for scenarios like this :-)

@atoader Looking forward to an update

Avanti05 avatar Mar 19 '18 13:03 Avanti05

@vytjak The compiler creates a separate class in C# for each record. These heirs of NavRecord. The communication e.g. between Codeunit and Record is managed by a NavRecordHandle object.

@atoader For the desired effect you would have to modify the compiler in such a way that it does not create two c# methods for a al method overload with diffrent records.The al overload with two different records must be combined into one method in C#. The generated code are already "record-independent".

Gallimathias avatar Mar 19 '18 14:03 Gallimathias