AL icon indicating copy to clipboard operation
AL copied to clipboard

A `[TryFunction]` that implements an interface returns wrong values

Open hemisphera opened this issue 2 years ago • 0 comments

1. Describe the bug When a [TryFunction] implements an interface that requests a function that returns a Boolean, the return value, when used through the interface does not return the correct value.

2. To Reproduce Create the following interface:

interface ITest
{
    procedure DoTest(): Boolean;
}

Create the following Codeunit that implements this interface

codeunit 50100 Implementation implements ITest
{
    [TryFunction]
    procedure DoTest();
    begin
    end;
}

Note that DoTest does not explicitly return a value here, even though this is requested by the interface. Being a [TryFunction] though, this implicitly is the case and as such this compiles without problems.

Also note that DoTest does nothing and as such cannot raise an exception. This in turn means that the function must return true, being a [TryFunction].

Create the following test codeunit:

codeunit 50101 ImplementationTest
{

    Subtype = Test;

    var
        Assert: Codeunit "Library Assert";

    [Test]
    procedure RunText();
    var
        interface: Interface ITest;
        codeunit: Codeunit Implementation;
    begin
        Assert.IsTrue(codeunit.DoTest(), 'Codeunit failed');
        interface := codeunit;
        Assert.IsTrue(interface.DoTest(), 'Interface failed');
    end;
}

The assertion "Codeunit failed" does not fail - this is expected The assertion "Interface failed" on the other hand does.

3. Expected behavior I expect one of two things:

  • either the code does not compile because a [TryFunction] is not a suitable implementation
  • or the interface correctly returns true or false correctly, according to if an error occurred or not.

4. Actual behavior You can run the test codeunit and see what happens.

5. Versions:

  • AL Language: v13.0.887907
  • Visual Studio Code: 1.81.1
  • Business Central: 23.0

hemisphera avatar Oct 25 '23 14:10 hemisphera