to nest or not to nest❓

如果在一個 generic type 裡面定義 nested types,那麼使用時就要特別小心,因為可能會產生下列問題:

⛔️ Generic parameter '...' could not be inferred.

建議用法:

  • 如果要定義的型別與 generic type 的 type parameter 高度相關,則適合作為 nested type。

  • 反之,如果與 type parameter 無任何關係,例如: MyGrid 例中的 Layout 就與 MyGrid 的 type parameter (Item, ItemView) 無任何關係,則不適合,因此 MyGrid 算是一個「反面教材」。

Last updated