flutter 为什么不能在Dart中分配泛型变量?

gjmwrych  于 2023-02-25  发布在  Flutter
关注(0)|答案(1)|浏览(118)

为什么不能在Dart中分配泛型变量?

void test<T1 extends num, T2 extends String>(T2 arg) {
    //    ↓ A value of type 'int' can't be assigned to a variable of type 'T1'.  Try changing the type of the variable, or casting the right-hand type to 'T1'.
    T1 t = 0;

    //     ↓↓↓↓ A value of type 'String' can't be assigned to a variable of type 'T2'.  Try changing the type of the variable, or casting the right-hand type to 'T2'.
    arg = 'Test';
}
oyxsuwqo

oyxsuwqo1#

下面的示例可能有助于理解泛型

print(add(10.5, 20));
T add<T extends num>(T a, T b) => a + b as T;

相关问题