Streams

Tim Peierls tim at peierls.net
Fri Sep 14 06:46:44 PDT 2012


On Fri, Sep 14, 2012 at 9:15 AM, David M. Lloyd <david.lloyd at redhat.com>wrote:

> I don't like it; I think it's going to result in things like:
>
> Map<String,Optional<List<**Optional<String>>>>
>
> or worse.


Only if you really work hard at obfuscating your code. I've been using a
version of Optional for about a year, and the only time I had reason to use
Optional as a type parameter was Callable<Optional<Result>>, which conveys
exactly what I mean: "Might have a result when it returns."

There's very little incentive (and a pretty daunting disincentive, in fact)
to do anything with an Optional besides test for the presence of a value
and get that value from it. Optional has (or should have) convenience
methods to get default values in the event that you do *not* want a
different code path:

    Result result = filteredResults.findFirst().or(defaultResult);

In such cases, you don't even mention the Optional type explicitly.

Calling it a matter of style is misleading: It helps prevent user errors,
and that's a very desirable property. The experts here might be disciplined
enough not to need such help, but we can't assume that everyone using these
libraries will be that disciplined.

One of the things that appealed to me about Java early on was the sense
that if I could get the code to compile, it would just work. My experiences
have fallen short of that ideal over the years, of course, but with
Optional I've had recent moments where it has come gratifyingly close.

--tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/lambda-libs-spec-experts/attachments/20120914/28f7b03a/attachment.html 


More information about the lambda-libs-spec-experts mailing list