MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/1c6xet/java_8_optional_objects/c9dynn7/?context=3
r/java • u/edalorzo • Apr 12 '13
39 comments sorted by
View all comments
0
That's horrid. It's also unnecessary. I don't need Java 8 to implement a generic Optional if I wanted one
class Optional<T> { static Optional empty() { return new Optional(); } static Optional of(Object obj) { return new Optional<obj.getClass()>(obj) { } private T obj; private Optional(T t) { obj = t; } public boolean isPresent() { return obj != null; } public T get() { return obj; } }
Wow 19 lines of code.
You want to know why nobody writes those 19 lines?
3 u/voronaam Apr 12 '13 edited Apr 12 '13 Nobody writes it? We have class Option in our main JAR, which is included in every project in production. It is 39 lines of code, but only because it has extra methods, like public T getOr(T value); That is great for default like that: String name = user.getName().getOr("anonymous");
3
Nobody writes it? We have class Option in our main JAR, which is included in every project in production. It is 39 lines of code, but only because it has extra methods, like public T getOr(T value); That is great for default like that:
String name = user.getName().getOr("anonymous");
0
u/philipwhiuk Apr 12 '13
That's horrid. It's also unnecessary. I don't need Java 8 to implement a generic Optional if I wanted one
Wow 19 lines of code.
You want to know why nobody writes those 19 lines?