Created
January 31, 2020 18:33
-
-
Save leonsenft/a8a92c56149499dd479488f1cca5bff9 to your computer and use it in GitHub Desktop.
Demonstrates that type inference is dependent on order of type parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Value {} | |
class Renderer<T> {} | |
// When V is defined before R, type inference works in all cases. | |
class WorkingOrder<V extends Value, R extends Renderer<V>> {} | |
// When V is defined after R, type inference fails in some cases. | |
class BrokenOrder<R extends Renderer<V>, V extends Value> {} | |
void main() { | |
// Correctly infers type. | |
var working1 = WorkingOrder(); | |
print(working1.runtimeType); // WorkingOrder<Value, Renderer<Value>>. | |
// Correctly infers type arguments to bounds. | |
WorkingOrder working2 = WorkingOrder(); | |
print(working2.runtimeType); // WorkingOrder<Value, Renderer<Value>>. | |
// ERROR: Incorrectly infers BrokenOrder<Renderer<dynamic>, Value>. | |
var broken = BrokenOrder(); | |
// Correctly infers type arguments to bounds. | |
BrokenOrder working3 = BrokenOrder(); | |
print(working3.runtimeType); // BrokenOrder<Renderer<Value>, Value>. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment