AL icon indicating copy to clipboard operation
AL copied to clipboard

Comparision of GUID and Text results in Runtime Error (Compile Time would be better)

Open pri-kise opened this issue 4 years ago • 6 comments

1. Describe the bug A GUID cannot be compared with Text, but i doens't results in a compile error.

2. To Reproduce Steps to reproduce the behavior:

  1. Create some Code with the following in it.
    local procedure DummyGuidCheck()
    var
        DummyGuid: Guid;
        DummyText: Text;
    begin
        if DummyGuid <> DummyText then;
    end;

3. Expected behavior I ecpect to receiv a Compiler Error instead of a Runtime Error

4. Actual behavior Right now the Code above is compiling but results in a runtime error, since text and guid cannot be compared.

5. Versions:

  • AL Language: 9.0.519128 (Preview Release)

  • Visual Studio Code: Version: 1.60.2 (user setup) Commit: 7f6ab5485bbc008386c4386d08766667e155244e Date: 2021-09-22T12:00:31.514Z Electron: 13.1.8 Chrome: 91.0.4472.164 Node.js: 14.16.0 V8: 9.1.269.39-electron.0 OS: Windows_NT x64 10.0.19042

  • Business Central: Version: W1 20.0 (Plattform 20.0.30192.0 + Anwendung 20.0.30934.0)

Final Checklist

Please remember to do the following:

  • [X] Search the issue repository to ensure you are reporting a new issue

  • [X] Reproduce the issue after disabling all extensions except the AL Language extension

  • [X] Simplify your code around the issue to better isolate the problem

pri-kise avatar Oct 06 '21 05:10 pri-kise

Hi @pri-kise, this is a Runtime Error because the text type variable has not been initialized when compared with the GUID. If you try the following example you will not get any error and the comparison will be done correctly.

  page 50101 TestingPage
  {
      PageType = Card;
      layout
      {
          area(content)
          {
              group(General)
              {
                  field("No."; MyProcedure())
                  {
                  }
              }
          }
      }
  
      local procedure MyProcedure(): Integer
      var
          DummyGuid: Guid;
          DummyText: Text;
      begin
          DummyText := '{12345678-0000-1111-2222-123456789012}';
          if DummyGuid <> DummyText then
              exit(10);
          exit(0);
      end;
  }

Jose-agg avatar Oct 19 '21 15:10 Jose-agg

What is the mysterious runtime error? Does it just say that Guid and Text cannot be compared, indicating at all? If the reality is that a well-formed Text can be compared with a Guid, the error should be clear about that, instead of implying it's never possible.

I guess it makes sense to be able to compare Guid with Text, as how else would one easily express an arbitrary GUID in code - but then I'd expect an error like 'The text is not in a valid GUID format', etc.

dzzzb avatar Oct 19 '21 15:10 dzzzb

Well to be clear. Maybe my repo wasn't optimal. We had problems with a Guid comparision to empty text, since we changed one field from text to guid.

    local procedure DummyGuidCheck()
    var
        DummyGuid: Guid;
    begin
       Evaluate(DummyGuid, '{12345678-0000-1111-2222-123456789012}');
        if DummyGuid <> '' then;
    end;

pri-kise avatar Oct 19 '21 15:10 pri-kise

@dnpb If such an error occurs, the compiler indicates that it is due to a format error and which format needs to be followed to avoid the problem as can be seen in this image. image

On the other hand, @pri-kise, in the case that we compare the GUID with a variable we would not be able to validate that it is a correct format at Compile time and it would be only at Runtime where we could deal with it. Only in the case of literals can we check whether a format is valid during compilation, but at the moment it is not a top priority change to make.

Jose-agg avatar Oct 19 '21 16:10 Jose-agg

@Jose-agg Thanks for your answer. Mabye a codecop info message, that informs the developer to be careful about the comparision and try to use a GUID variable together with an evaluate statement could be an simple temporary solution until there is enough time for more complex solutions?

pri-kise avatar Oct 20 '21 06:10 pri-kise

Agreed, comparing with a literal should be a compile-time error, but in the meantime an info/warning that ideally a Guid type would be used instead, would be preferable and avoid accidental production problems like I'm seeing just now. :-(

dzzzb avatar Feb 22 '22 10:02 dzzzb