microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
719 stars 242 forks source link

Why AL0603 for assigning integers to an enum? #7698

Open NKarolak opened 3 months ago

NKarolak commented 3 months ago

Bug or feature? Why is warning AL0603 raised when directly assigning an integer to MyEnum, and why not when assigning it indirectly in foreach?

procedure TestAL0603()
var
    MyEnum: Enum MyEnum;
    Integers: List of [Integer];
begin
    // both raise warning AL0603:
    MyEnum := 1;
    MyEnum := Integers.Get(1); 

    // both raise no warning
    foreach MyEnum in MyEnum.Ordinals() do ...;
    foreach MyEnum in Integers do ...;
end;

Don't get me wrong - I prefer it without the warning. So much cleaner ... But then I see no reason why we should get AL0603 at direct assignment.

Versions:

dannoe commented 3 months ago

I think the correct way would be:

procedure TestAL0603()
var
    MyEnum: Enum MyEnum;
    Integers: List of [Integer];
begin
    MyEnum := MyEnum.FromInteger(1);
    MyEnum := MyEnum.FromInteger(Integers.Get(1));
end;

But you can not use FromInteger in foreach, at least not without some (bad looking) overhead:

procedure TestAL0603()
var
    MyEnum: Enum MyEnum;
    MyEnumAsInt: Integer;
begin
    foreach MyEnumAsInt in Enum::MyEnum.Ordinals() do begin
        MyEnum := MyEnum.FromInteger(MyEnumAsInt);

   end:
end;

So in my opinion, it's fine the way it is.