Stream proposal and null results
Stephen Colebourne
scolebourne at joda.org
Thu May 17 15:46:16 PDT 2012
On 11 May 2012 18:55, Brian Goetz <brian.goetz at oracle.com> wrote:
> Yes, you caught us :)
>
> Currently getFirst() is the only method that cheats in this way (using
> null as a sentinel), and this is generally a step in the wrong
> direction. A much more promising direction here would be to do
> something like Option (Guava) or Optional (Scala) where getFirst returns
> an Option, rendering it impossible for the user to "forget" to do the
> null check. And, of course, having the return type explicitly represent
> maybe-null is more self-documenting than the current prototyped
> approach. Moving to some sort of Option-like approach here is on our
> to-do list, but currently we're working through some bigger issues
> first, such as how the streams framework attaches to collections.
FWIW, I think that the Option<T> approach to null handling would be a
big mistake in Java.
What you end up with in that strategy is deeply nested generics like
Map<Optional<String>, List<Optional<Integer>>>
I have a guidance rule that 1 level of generics is OK, 2 is
manageable, but 3 means you're probably missing a real domain class.
Optional<T> just gets you to that 2nd/3rd layer of generics faster
than necessary. I'd also note that those nested generics tend to be
harder for developers to rationalise about.
Stephen
More information about the lambda-dev
mailing list