-
-
Notifications
You must be signed in to change notification settings - Fork 12
Description
When there's no explicit type argument on Scope.write(), it is compile time valid to pass a state that can't be attributed to ref. Take this example:
final countRef = StateRef(0);
class CounterLogic with Logic {
...
void increment() {
write(countRef, 'banana'); // compiler: yup, go ahead.
}
}This happens because, implicitly, T becomes the least upper bound type of both arguments, which is Object. Such conclusion implies that, more precisely, the problem is not about explicitly defining a type argument, since write<Object>(countRef, 'banana') is also compile time valid. The actual point is the way the compiler fails to spot this particular incorrect construction.
The only way I see to solve this up is by not declaring both state and ref as parameters of the same method. I'd consider something like the following:
abstract class Scope {
StateRefHandler<T> onStateRef<T>(StateRef<T> ref);
...
}
abstract class StateRefHandler<T> {
void write<T>(T state, [Object action]);
...
}There's a clear complexity increase on both library implementation and use, besides being a breaking change, but I believe it's worth it.
An alternative is discouraging calling Scope.write() without explicit type arguments. Perhaps a summary of this discussion could be placed somewhere at the documentation.