Function Overloading in AL

I haven’t been blogging much, lately. But in my defence – I put quite some time in another community project, which I blogged about in my previous post (and will blog about in quite a few coming posts…). To keep up the pace of writing at least one post each month (which I have been able to do so for the past 11 years or so) – I HAD to put something up urgently ;-). And it’s going to be about a new possibility we have in the AL language, which is often forgotten:

Function Overloading

In C/AL we never had the ability for overloading a function. You know: using the same function name with a different context (parameters).

Now – YES WE CAN! It’s all pretty well described on docs:

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods/devenv-overload-method

How cool is that?

Can I overload more?

Overloading my own functions is nice. But the question I always get in my classes is: can we also overload functions from the default app – or from any dependent app for that matter.

Well – in short: YES WE CAN!

Let me give you a simple example:

I’m overloading the default method “CreateAndShowNewInvoice” on the vendor table with a different context (Text-parameter). So I have a completely new method, with the same name.

One thing that would be interesting is to be able to extend on a main method like that – and that’s what I basically doing above – I call the main method, and extend it with my own code.
Dependencies

As this works on the default app, you can expect it does work on dependent extensions as well. I created an example on my github, which you can look into.

Extension A implements a new method on the Item table (or should I say “class” ;-)):

Extension B has a dependency on A, and implements a slightly different implementation of that method. You see: same function name, different parameters

To Test the overload, I created a subscriber to logInManagement .. And you’ll see that B basically extended the implementation of A…

And yes, I hear you – One should test with a test-codeunit. So I created that for you as well :-).

And if you’re using the CRS AL Language Extension, it’s easy. Just run the C/AL Test Tool from VSCode from the command palette:

And there you go! 🙂

Enjoy!

5.00 avg. rating (98% score) - 4 votes

Permanent link to this article: https://www.waldo.be/2018/05/29/function-overloading-in-al/

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.