AL icon indicating copy to clipboard operation
AL copied to clipboard

Cannot convert Variant to List

Open konstantinlevchenko opened this issue 7 years ago • 11 comments

When I'm trying to assign Variant to List, I'm getting following error in VS Code: Cannot implicitly convert type 'Variant' to 'List of [Text]'

Here is an example how to reproduce this error: local procedure ListToVariant() var SourceList: List of [Text]; SourceVariant: Variant; TargetList: List of [Text]; begin SourceVariant := SourceList; TargetList := SourceVariant; //Error: Cannot implicitly convert type 'Variant' to 'List of [Text]' end;

konstantinlevchenko avatar Aug 01 '19 15:08 konstantinlevchenko

Hi @konstantinlevchenko ! We don't currently support converting a variant to a List. Can you better describe your scenario? Is there something else that, if added to the language, would help you better achieve your goal?

atoader avatar Aug 02 '19 13:08 atoader

@atoader we can store most types in an array, arrays can hold variants, we can assign most types to variants but we can't check some of them or convert them back to the original type (IsList, IsDictionary, IsEnum, IsControlAddIn, IsPage, IsKeyRef, etc.). The docs just state: "The AL variant data type can contain many AL data types." and when there are types missing it kinda sticks out.

The workaround is to use an alternative supported structure for storing the List, like IsJsonArray, and iterate through the keys into a new List when retrieving it, which isn't very performant (and just feels unnecessary).

bjarkihall avatar Mar 03 '21 04:03 bjarkihall

Please go ahead and post this to our Ideas forum at https://aka.ms/BusinessCentralideas, or vote up the idea if its already there. We're constantly monitoring top Ideas and will consider them for a future release.

atoader avatar Mar 07 '21 13:03 atoader

@atoader it just looks more like something that was forgotten to add, instead of being a "new idea". You can assign list to a variant (without any compiler errors) but later you can't get it back again, isn't that considered broken?

I would submit a PR if I could:

// inside NavVariant class:

public bool ALIsList
{
  get
  {
    return this.IsOfNavType(NavType.NavList);
  }
}

internal void ALAssignNavComplexValue(NavValue other)
{
    this.Clear();
    switch (other.NclType)
    {
        //...
        case NavNclType.NavList:
            this.ClearAndSetValue(other);
            break;
    //...
}

// and same for NavDictionary, etc.

This also relates to the next release: https://docs.microsoft.com/en-us/dynamics365-release-plan/2021wave1/smb/dynamics365-business-central/returning-complex-types

If a procedure can accept any NavValue by accepting a Variant, it won't be able to return any NavValue by returning a Variant, right?

bjarkihall avatar Mar 07 '21 23:03 bjarkihall

You have a good point. In the initial implementation, assignment from List to Variant was not allowed. It seems that we allowed it at some point, but only partially. In this case, it is a bug.

atoader avatar Mar 09 '21 15:03 atoader

@atoader, can you update a milestone for this one?

vody avatar Jun 14 '23 19:06 vody

atoader left Microsoft already in 2021 or 2022.

NKarolak avatar Jun 15 '23 06:06 NKarolak

@qutreson or @thpeder might be able to help :)

atoader avatar Jun 15 '23 12:06 atoader

@qutreson, any option to include Variant to Codeunit and Variant (with Codeunit) to Interface? Then my Service Provider concept will work. :)

vody avatar Jul 15 '23 16:07 vody

Support for getting lists/dictionaries out of a variant will come with the next major release of the compiler, or one of the pre-release versions if someone wants to try it early.

BazookaMusic avatar Jul 31 '23 14:07 BazookaMusic

@qutreson, any option to include Variant to Codeunit and Variant (with Codeunit) to Interface? Then my Service Provider concept will work. :)

Unfortunately, we are not looking into allowing this yet.

BazookaMusic avatar Jul 31 '23 14:07 BazookaMusic