Skip to content

Instantly share code, notes, and snippets.

@DeepSpawn
Created September 14, 2016 22:27
Show Gist options
  • Save DeepSpawn/7fb7359960751956b15c52033339c290 to your computer and use it in GitHub Desktop.
Save DeepSpawn/7fb7359960751956b15c52033339c290 to your computer and use it in GitHub Desktop.
//Given
interface CheckedFunction<T, R> {
R apply(T t) throws Exception;
}
//Does not compile
public <U> Try<U> flatMap(final CheckedFunction<? super T, Try<U>> f) {
return f.apply(value);
}
//Breaks laws on the 'x -> throws execption' case
public <U> Try<U> flatMap(final CheckedFunction<? super T, Try<U>> f) {
try {
return f.apply(value);
} catch (Exception e) {
return Try.failure(e);
}
}
//is unpleasant to use
public <U> Try<U> flatMap(final CheckedFunction<? super T, Try<U>> f) throws Exception {
return f.apply(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment