Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

GD0302:泛型型別參數必須標註為 [MustBeVariant] 屬性

規則 ID

GD0302

分類

用法

修正是否會破壞相容性

破壞性變更

預設啟用

原因

指定的泛型型別參數不是預期的 Variant 相容型別,且該泛型型別未標註 [MustBeVariant] 屬性。

規則說明

當泛型型別參數被標註為 [MustBeVariant] 屬性時,該型別必須為 Variant 相容型別。若使用的型別也是泛型型別,則該泛型型別也必須標註 [MustBeVariant] 屬性。例如泛型 Godot.Collections.Array<T> 只允許可轉換為 Variant 的型別項目,如果泛型型別本身有正確標註即可。

public void Method1<T>()
{
    // T is not valid here because it may not a Variant-compatible type.
    var invalidArray = new Godot.Collections.Array<T>();
}

public void Method2<[MustBeVariant] T>()
{
    // T is guaranteed to be a Variant-compatible type because it's annotated
    // with the [MustBeVariant] attribute, so it can be used here.
    var validArray = new Godot.Collections.Array<T>();
}

修正方式

要修正此規則的違規情形,請將 [MustBeVariant] 屬性加入必須 Variant 相容的泛型型別。

什麼時候應該忽略警告

請勿忽略此規則的警告。標註 [MustBeVariant] 屬性的泛型參數通常會傳遞至引擎,若型別無法被正確處理,將導致執行階段錯誤。