Bug 416304 - [compiler][null] @DerivedNullable
Summary: [compiler][null] @DerivedNullable
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows Vista
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-09-01 02:30 EDT by Ed Willink CLA
Modified: 2013-09-02 16:23 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ed Willink CLA 2013-09-01 02:30:30 EDT
A common pattern I've encountered could be termed derived nullable.

Given a function f(A) returning B, I have a dilemma in declaring A and B as @Nullable or @NonNull when I want the behavaiour

non-null A => non-null B
null A => null B

@Nullable B f(@Nullable A) gives me no checking at all
@NonNull B f(@NonNull A) prohibits null propagation

Suggestion:

@DerivedNullable B f(@Nullable A a)

where @DerivedNullable is equivalent to

@NonNull if all parameters of f are known to be non-null
@Nullable otherwise

Or more powerful/complex

@DerivedNullable(a) B f(@Nullable A a)

where the @DerivedNullable arguments provide a conjunction of non-null requirements.
Comment 1 Stephan Herrmann CLA 2013-09-02 16:23:05 EDT
That's an interesting one!

Let me see, if we were able to say this:

  <N extends Nullness> @N B f(@N A a)

(where @NonNull and @Nullable would both extend @Nullness).

wouldn't that meet your requirement?

While neither javac8 nor ecj8 accept such code, interestingly, they reject it for
different (incorrect?) reasons.

Before discussing a solution in Java7- land, let me first check, whether anything
along the sketched lines is _intended_ to work in Java8 (though I guess it would not).