Cannot convert Variant to List
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;
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 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).
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 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?
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, can you update a milestone for this one?
atoader left Microsoft already in 2021 or 2022.
@qutreson or @thpeder might be able to help :)
@qutreson, any option to include Variant to Codeunit and Variant (with Codeunit) to Interface? Then my Service Provider concept will work. :)
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.
@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.