Closed GoogleCodeExporter closed 9 years ago
Why wouldn't you just write
T t = clas.cast(o);
...since the Class.cast method would throw the ClassCastException for you?
Original comment by wasserman.louis
on 17 Jun 2013 at 12:55
Normally, you would, but for the sake of showing some example, I would write it
the other way.
Original comment by jamie.sp...@gmail.com
on 17 Jun 2013 at 1:08
Generally:
T t = guard(predicate).apply(o).transform(function)
.orElseThrow(throwableSupplier);
vs
T t;
if (!predicate.apply(o))
throw x;
t = function.apply(o);
Original comment by jamie.sp...@gmail.com
on 17 Jun 2013 at 1:12
This seems like a duplicate of issue 856 in many ways...
As usual, I'm leery of adding lots more Function and Predicate sugar. In all
honesty, I find the second example you give more readable than the first --
except I wouldn't bother with the Predicate and the Function objects, I'd just
make them normal method invocations:
if (!good(o)) {
throw new FooException();
}
T t = bar(o);
Original comment by lowas...@google.com
on 17 Jun 2013 at 4:59
Honestly, I'm more concerned with the fact that I can do a lot more with an
Optional, in general. I'd just like to have a method for throwing an exception
given that there are methods for providing default values from instances and
Suppliers, and even passing other Optional. I don't care as much for guava
providing Functions.guard(); it's just the means in which to provide a test;
just so long as the result yields an Optional.
I'd feel as if Optional.or(Supplier<? extends T> s) does just as much as
Optional.orElseThrow(Supplier<? extends X> x) throws X would.
imperative:
return optional.isPresent() ? optional.get() : s.get();
if (!optional.isPresent())
throw x.get();
return optional.get();
functional:
optional.or(s);
optional.orElseThrow(x);
I feel as if I'd use it a lot, as I like using Suppliers when I can, and I like
having objects around more so than static utilities.
Original comment by jamie.sp...@gmail.com
on 17 Jun 2013 at 7:02
Optional.orElseThrow was rejected in issue #1121.
On the other hand it's part of
[http://download.java.net/jdk8/docs/api/java/util/Optional.html#orElseThrow(java
.util.function.Supplier) Java 8's Optional].
Original comment by xaerx...@gmail.com
on 6 Sep 2013 at 10:15
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<issue id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:12
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:17
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:08
Original issue reported on code.google.com by
jamie.sp...@gmail.com
on 17 Jun 2013 at 12:30