GD0301.rst 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. GD0301: The generic type argument must be a Variant compatible type
  2. ===================================================================
  3. ==================================== ======================================
  4. Value
  5. ==================================== ======================================
  6. **Rule ID** GD0301
  7. **Category** Usage
  8. **Fix is breaking or non-breaking** Breaking
  9. **Enabled by default** Yes
  10. ==================================== ======================================
  11. Cause
  12. -----
  13. An unsupported type is specified for a generic type argument when a
  14. :ref:`Variant-compatible type <c_sharp_variant_compatible_types>` is expected.
  15. Rule description
  16. ----------------
  17. When a generic type parameter is annotated with the ``[MustBeVariant]`` attribute,
  18. the generic type is required to be a Variant-compatible type. For example,
  19. the generic ``Godot.Collections.Array<T>`` type only supports items of a type
  20. that can be converted to Variant.
  21. .. code-block:: csharp
  22. class SomeType { }
  23. // SomeType is not a valid type because it doesn't derive from GodotObject,
  24. // so it's not compatible with Variant.
  25. var invalidArray = new Godot.Collections.Array<SomeType>();
  26. // System.Int32 is a valid type because it's compatible with Variant.
  27. var validArray = new Godot.Collections.Array<int>();
  28. How to fix violations
  29. ---------------------
  30. To fix a violation of this rule, change the generic type argument to be a
  31. Variant-compatible type or use a different API that doesn't require the generic
  32. type argument to be a Variant-compatible type.
  33. When to suppress warnings
  34. -------------------------
  35. Do not suppress a warning from this rule. API that contains generic type arguments
  36. annotated with the ``[MustBeVariant]`` attribute usually has this requirement
  37. because the values will be passed to the engine, if the type can't be marshalled
  38. it will result in runtime errors.