-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow setters to declare a return type of Never
#4202
Comments
The return type of a setter doesn't matter, because the type isn't used anywhere in the specification. If we allow
Could work. |
Yes, it would only be useful for instance setters, and only for a few cases. I feel like there are no downsides (except, obviously, for the resources spent to implement it), but maybe the benefits are not worthy enough. I was thinking |
This comment was marked as off-topic.
This comment was marked as off-topic.
I think it is, just wanted to be certain that it was included since "setter" sometimes means "something declared using |
One workaround is to mark such setter with a @Deprecated('The list is immutable, do not use');
@override
void operator[]=(int index); |
This is an interesting workaround and may work, although the semantics conveyed are not correct. |
I agree that allowing setters to use class UnmodifiableListView<T> {
List<T> _items;
UnmodifiableListView(this._items);
Never operator []=(int index, T value) {
throw UnsupportedError('Cannot modify an unmodifiable list.');
}
}
void main() {
var list = UnmodifiableListView<int>([1, 2, 3]);
// Trying to assign a value will always throw an error
list[0] = 10; // Throws UnsupportedError
} |
This may be only useful on some few edge cases, so I am not sure if this is viable or not, but it may not be so hard to do and isn't breaking.
Currently, the specification explicitly states:
With null-safety and the
Never
type introduced, there are cases where one might want to change their setter return type to indicate that an error is never thrown.For instance,
UnmodifiableListView.operator []=
always throw.It could be useful to be able to declare the return type of those kinds of setters as
Never
.The text was updated successfully, but these errors were encountered: