PROPOSAL : Specify nullable argument
Reinier Zwitserloot
reinier at zwitserloot.com
Tue Mar 17 17:27:33 PDT 2009
Adding non-nullity to the type system would be a great boon, IMO, but
its far more complicated than just having a @NonNull annotation.
I've written up a proposal a while ago here: http://www.zwitserloot.com/2008/10/23/non-null-in-static-languages/
Suffice to say it gets very complicated because you need 4 nullity
states (Never-Allows-Null, Definitely-Allows-Null, Might-Allow-Null,
Legacy (like raw generics, for interop with old code), *AND* you need
a separate process to promote or demote generics (So that you can say:
Eventhough this is a Map that maps NonNull String to NonNull String,
if you call get() on this, you get a @Nullable String back, because
null will flow out if I can't find the key. In other words, the 'V'
generics parameter needs to be demoted to @Nullable V somehow).
Read the blog entry for a more elaborate explanation with examples.
I'd love something like that in java, but the proposal as it stands
needs a source 1.7; keyword (because having no modifier is taken as
'never null', which is obviously not backwards compatible), and is
very complicated, so: Definitely not project coin material.
Incidentally, I think jsr305 is fundamentally broken. It doesn't have
the states needed to convey nullity info; it needs at least 2
annotations, probably 3 to be able to differentiate between code that
simply didn't specify anything, and code that did, instead of the one
annotation that jsr305 gives.
--Reinier Zwitserloot
On Mar 17, 2009, at 21:41, Neal Gafter wrote:
> Did you intend this to be a complete proposal?
>
> Do you like this better than @NonNull, which will probably be included
> in Java 7 under jsr 305?
>
> On Tue, Mar 17, 2009 at 1:25 PM, Olivier Chorier
> <lapsus63 at gmail.com> wrote:
>> Example :
>>
>> public void getListOfProducts(Company owner, !String facultativeName)
>> {
>> ....
>> }
>>
>> The '!' should prevent the developper that the argument is able to
>> be null.
>> Invoking getListOfProducts(null, "name") could throw a compilation
>> error/warning.
>>
>> Or maybe better :
>>
>> public void getListOfProducts(!Company owner, String facultativeName)
>> {
>> ....
>> }
>>
>> Here, the '!' indicates a mandatory argument (not null).
>>
>>
>> However, I don't know if the '!' marker could be the most
>> appropriate.
>>
>>
>
More information about the coin-dev
mailing list