'Explict generic type in function by default replace this type to dynamic inside
Why explict generic type in function by default replace this type to dynamic inside?
example:
class Boo {
void displayType<int>() {
print('type int to string: $int');
print('type string to string: $String');
}
}
main() {
final boo = Boo();
boo.displayType();
}
output:
type int to string: dynamic
type string to string: String
its bug?
Solution 1:[1]
name of generic can be existing type. So if tell any type in function, inside int
can be any another type
main() {
final boo = Boo();
boo.displayType<Boo>();
}
type int to string: Boo
type string to string: String
Solution 2:[2]
Here <int>
is not an int
data type but a name of generic datatype to be passed into function.
void displayType<int>() {
print('type int to string: $int');
print('type string to string: $String');
}
Compliler builds priorities from global to local. That means - it will prioritize local variables, arguments, generic types more than global ones. For example:
int number = 2;
void someFunc(){
//local variables are in higher priorities inside their closures
int number = 3;
//will print 3
print(number);
}
You definded generic type as <int>
- compiler takes it as a priority in assgning and usage operations above the actual data type named int
. Knowing this - do not be confused and make generic types consisting of 1 letter as usually it is done in many documentations.
void displayType<T>() {
//now it prints int
print('type int to string: $int');
//will print dynamic
print('type generic to string: $T');
print('type string to string: $String');
}
main() {
final boo = Boo();
boo.displayType();
}
Solution 3:[3]
You function declaration
void displayType<int>() {
print('type int to string: $int');
print('type string to string: $String');
}
introduces a generic function with a type parameter named int
.
It's exactly the same as
void displayType<T>() {
print('type int to string: $T');
print('type string to string: $String');
}
except that the type parameter is named int
, which also shadows the declaration of int
normally imported from dart:core
.
When you then call that method without a type argument, as
boo.displayType();
the type argument T
/int
(name doesn't matter) is inferred by using the bound of the type parameter (because there are no method arguments to infer a type from either). It has no bound given, so it defaults to dynamic
.
It's not at all clear what you are trying to do.
If you wanted a type parameter bounded by int
, you can write
void displayType<T extends int>() {
print('type T to string: $T');
print('type int to string: $int');
print('type string to string: $String');
}
If you are trying to override a superclass method which is also generic, to not be generic, you can't do that. The override must have the same type parameters with the same bounds as the superclass, otherwise the override is invalid.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | anweledig |
Solution 2 | |
Solution 3 | lrn |