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
Add Option type to Dart #10422
Comments
Added Area-Language, Triaged labels. |
This comment was originally written by bord...@gmail.com If you decided to implement this, I'd recommend making Option<T> implement Iterable<T> and more tightly defining the return types of the methods that return Iterable to return Option<T>. I find the easiest way for users to reason about option is to present it as the an Iterable that contain 1 or 0 elements. |
This comment was originally written by mario.plon...@gmail.com I'm hoping opinions are allowed here: I really hope this "feature" as it is described doesn't make it into Dart, or at least get their own Map and List classes. Why? Because it actually makes code more verbose, without any additional benefit. If I'm just getting something wrong here, please correct me (mail is fine too). I've tried to write code as I would do it two different ways, once with Dart as it is, and once with a hypothetical option type, using the examples and descriptions above. // not really that much of a difference // current way is even better? key presses in dart editor (without shift): 60 : 66 So, there isn't really much difference in "coding speed". map["key"].doSomething(); That's a lot of additional code, compared to previous use-cases. |
This comment was originally written by mario.pl...@gmail.com Why don't you just use a wrapper, if this behaviour is needed? class Option<T> { class OptionList<T> extends ListBase<T> { void main() { |
This comment was originally written by kurisu....@gmail.com @mario, the use case isn't as much "speed of coding." It's mostly about correctness. Nullable types have been considered by most of the PLT community I know (and plenty of programmers in the Industry too!) as the biggest mistake in computing, because it makes everything so much more error prone — programmers are lazy ;) When you can't have nulls creeping throughout all of your code base, you can not possibly have NullPointerException, and you're forced to be remembered that you're dealing with a computation that might fail, and handle the errors accordingly. When any type can also be null, someone might change something in an library you're using, and you will only realise things broke when you start getting random NullPointerExceptions far down the road where they've actually happened! |
As others have noted, you can definitely define your own Option class at the library level, and there are a number of third party ones floating around the ecosystem. We don't currently have any plans to bake an option type into the core libraries. Instead, we're going to pursue supporting non-nullable types directly (#22). If that doesn't pan out, we may revisit this. But, until then, I'm going to close this as not planned. |
This issue was originally filed by bordole...@gmail.com
I think it would be very useful to add an Option type (http://en.wikipedia.org/wiki/Option_type) to dart, particularly for when dealing with collections and maps, and when methods on any object are not guaranteed to return a non-null result [*].
For instance, the Map operator [] currently returns null if the key is not present. This forces users of Map to write fairly verbose error checking and is bug prone.
Example:
main() {
Map<String, String> map = new Map();
String s = map["key"];
print(s.endsWith("example")); // noSuchMethodException
}
To fix this code, you need to remember to add null checks throughout your code and the compiler does not give any warnings of the potential bug.
an alternative is to instead return an Option type and call the method map:
main() {
Map<String, String> map = new Map();
Option<String> opt = map["key"];
opt.map((String s) => print(s.endsWith("example"))); // Only called if s is non null
}
In addition, many of the Iterable methods currently throw RangeError or StateError and could be changed to instead return Option. This is useful since unchecked exceptions typically are not handled by programmers and can be tricky to debug.
Example
main() {
Iterable<String> itr = [];
String item = itr.elementAt(2); // RangeError, need to remember to add exception handling
print(item);
}
The alternative with the Option type is:
main() {
Iterable<String> itr = [];
Option<String> opt = itr.elementAt(2); // Returns None since the element does not exist
opt.map((String item) => print(item)); // Does nothing
print(opt.orElse("default")); // prints default
}
[*] I think this is a different from the proposal in https://code.google.com/p/dart/issues/detail?id=22 for non-nullable types.
The text was updated successfully, but these errors were encountered: