You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Replace the nullable annotations with guava's Optional class. #9
Probably the single biggest disadvantage of null is that it's not obvious what it should mean in any given context: it doesn't have an illustrative name. It's not always obvious that null means "no value for this parameter" -- heck, as a return value, sometimes it means "error", or even "success" (!!), or simply "the correct answer is nothing". Optional is frequently the concept you actually mean when you make a variable nullable, but not always.
/** * Returns the value specified by the given key. * * @param key The key * @return The value, which may be null, including when the key doesn't exist */publicOptional<Object> get(Objectkey) {
if (!locals.containsKey(key)) Optional.absent();
returnOptional.of(locals.get(key));
}
/** * Get an object whose key will be the object's class. * * @param key The key * @param <T> The type of object * @return The value */@SuppressWarnings("unchecked")
public <T> Optional<T> get(Class<T> key) {
if (!locals.containsKey(key)) Optional.absent();
returnOptional.of((T)locals.get(key));
}
I don't use Guava's Optional because it's not a proper monad and so you end up writing a lot of boilerplate code everywhere to deal with Optionals. I prefer Java 8's Optional, but it's too early to require Java 8 (plus optionals/monads are still a pain to work with prior to lambdas).
Activity
sk89q commentedon Jun 25, 2015
I don't use Guava's Optional because it's not a proper monad and so you end up writing a lot of boilerplate code everywhere to deal with Optionals. I prefer Java 8's Optional, but it's too early to require Java 8 (plus optionals/monads are still a pain to work with prior to lambdas).
nikosgram commentedon Jun 25, 2015
Ow, ok.
sk89q commentedon Jun 25, 2015
I was going to leave the ticket open in case there was a demand to switch over.
nikosgram commentedon Jun 25, 2015
I do not think there is demand to switch over, but okay. 😄
twizmwazin commentedon Jun 25, 2015
I would prefer use of the Optional class over nullables.
onbjerg commentedon Aug 7, 2015
👎