C# - Cannot set `Variant` as value of Dictionary
Godot version
v4.0.beta1.mono.official [4ba934bf3]
System information
Windows 11 21H2 (OS Build 22000.978)
Issue description
When trying to set the value of a Godot.Collections.Dictionary to a Godot.Variant.Type value (ulong), see line 3:
var d = new Godot.Collections.Dictionary();
d.Add("name", "property_name");
d.Add("type", Variant.Type.String);
it produces the following error:
Argument type 'Godot.Variant.Type' is not assignable to parameter type 'Godot.Variant'
I would expect an implicit conversion.
Steps to reproduce
- Create a new C# Class
- Create a method containing the following code:
var d = new Godot.Collections.Dictionary();
d.Add("name", "property_name");
d.Add("type", Variant.Type.String);
Minimal reproduction project
We implement implicit conversions from marshable types to Variant, enums are marshable as their underlying type but since each enum is considered a different type we'd have to implement implicit conversions for every enum and for user-defined enums this would not be possible.
I think the best thing we can do here is add an analyzer so when you are assigning an enum to a Variant we let you know that you need to cast it (we could also provide a Code Fix that adds the cast for you).
In terms of providing conversions for engine provided enums: would you think it makes sense and is purely not done because it's a lot of work? Or would you say it's not good because it would create inconsistencies with how userland enums would behave?
I like the idea with the source generators and quick fixes! That definitely scales and would make things very obvious for users. Should that be a feature request instead?
I think the inconsistency would certainly make it confusing since some users would wonder why it works with some enums and not others, but it's more to avoid a tight coupling between the Variant type and the rest of the engine.