Nulls
Brian Goetz
brian.goetz at oracle.com
Sun Sep 23 08:53:34 PDT 2012
Suppose we were designing the foreach loop. Would we consider
generating code in the compiler-generated loop header to skip over
nulls? If we didn't do that, why should
for (T t : collection) { blah(t) }
and
collection.stream().forEach(t -> { blah(t) })
have different behaviors? (ObNitpicking: They are different in the
sense that the lambda can't capture mutable up-level locals, and the
foreach loop can.)
People write foreach loops all the time that will behave badly if the
induction variable is null. People will write lambdas with the same
characteristics.
If we are doing to do anything special for nulls, I would prefer to ban
them/fail-fast, but people have argued that is impractical. Second best
seems to be not trying to do anything special to protect people from
them or imbue them with any special characteristics?
On 9/23/2012 10:33 AM, Tim Peierls wrote:
> On Sun, Sep 23, 2012 at 10:11 AM, Doug Lea <dl at cs.oswego.edu
> <mailto:dl at cs.oswego.edu>> wrote:
>
> So unobvious that I still don't see it. As long as you
> aren't treating null as
> an acceptable value in Streams or as the contents of an
> Optional, why do you
> need to re-check?
>
>
> But in Brian's proposal, null IS acceptable.
>
>
> It's tolerated, but what we're talking about -- are we not? -- is the
> extent to which this toleration distorts the overall design.
>
> So when I said "Doesn't bother me", I meant that the implications you
> point out for null-obliviousness -- e.g., findAny "lying"; repeating
> Map.get anti-pattern -- aren't important enough to me to warrant
> abandoning the proposed rules:
>
> * Streams are completely null-oblivious (we don't treat them
> specially at all)
>
> * Option is null-hostile.
>
> Yes, there's a risk that people won't get the "don't use nulls in your
> collections" memo, but as you point out by bringing up Map.get, it's
> something that folks have been dealing with for years. At least the ones
> who do get the memo won't be punished for the potential sins of those
> who don't.
>
> --tim
More information about the lambda-libs-spec-observers
mailing list