Repository navigation
Description
(This is not a priority at the moment. I just saw reference to it, so I wanted to capture it in an issue.)
I'm writing this issue under the assumption that defaulting annotations are likely to be permitted on methods (#43) (though I suppose we could ask this issue's question regardless). This issue is about even smaller scopes that that.
Previously:
- Common pattern for a set of exclusive scope-annotations (e.g. null-marked and null-unmarked) #109 (comment) and Common pattern for a set of exclusive scope-annotations (e.g. null-marked and null-unmarked) #109 (comment) briefly mentioned the possibility of applying defaulting annotations to individual parameters.
- The following is not really the same thing, but: Name(s) of the annotation(s) expressing "in the nullness club" or "not" #87 (comment) suggested that a single annotation could act as both a defaulting annotation and a type annotation (though I think that's probably not going to happen).
We could ask this question both about nullness specifically and about possible future annotation "domains." (But note that it makes sense only the case of type annotations: Non-type annotations might well apply to PARAMETER or TYPE_PARAMETER, but I see them as a distinct case.)
I think the answer is probably "no" for PARAMETER. I think there's a more interesting case for TYPE_PARAMETER, so I wouldn't dismiss it out of hand. But it has downsides, too, and certainly I don't see it as a priority at the moment.
I had briefly been excited about PARAMETER: It seemed like a way to annotate an individual method parameter as "not nullable" without needing a @NonNull annotation. Sure, it's slightly inelegant (especially if the defaulting annotation has an awkward name) and slightly indirect, but it should pretty much do what people want. It's actually potentially nicer than @NonNull in some cases: First, you could write @NullAware Map<Foo, Bar>, and that would apply to all 3 components of the type. Second, you could write @NullAware T, and there wouldn't be a question of whether type that "projected" T to a non-nullable type or not.
But... I think this may lead users to think of the defaulting annotations as applying to a type (well, really to a tree of types). As a result, users might think they can put @NullAware on "the return type of" a method. [edit: especially because many existing artifacts of nullness annotations use that location to apply to the return type!] But in fact, @NullAware on a method is likely (#43) to apply to the entire method -- parameters included.
I think this is a good argument for putting defaulting annotations only on "scopes."
(A small part of me still wants to make an exception for TYPE_PARAMETER: It's much more distinct than the parameter-vs.-return case. And it's still potentially a handy way to annotate an individual type or two as non-nullable. But again, I don't know whether I'd want to push for this or not.)
Activity
kevin1e100 commentedon Sep 28, 2020
Ah yes, exempting a method parameter. A related point here is that something like
Map<@NonNull String, @NullnessUnspecified List<@NonNull String>>seems "weird": how could something inside an unspecified qualifier be specified? I think it makes technical sense, though I'm unclear anyone would want to write this, or at least I'm unsure that's what they meant when they actually wrote:Did the author really mean for
List's type argument to be implicitly@NonNull String? This is a reason why I have been liking the idea of not introducing@NullnessUnspecifiedas a type annotation, only relying on defaulting instead for that.That is to say, assuming the author meant
@NullnessUnspecified List<@NullnessUnspecified String>, it would arguably be clearer if the author had no choice but to write@DefaultNullnessUnspecified List<String>(modulo annotation name).Still, allowing defaulting annotations in locations like that seems to run counter to the idea of allowing them on (some kinds of) "scopes": there is no scope in the Java scope nesting sense when using a default annotation as a type annotation. Method parameters and type parameters likewise I don't believe constitute a scope, and because of a similar concern I believe we had tentatively ruled out default annotations on fields (where I see a stronger case personally).
cpovirk commentedon Sep 28, 2020
Thanks for mentioning
FIELD. That got me to look for the past issue about it. It was #50, and I see that it actually coveredPARAMETER, too. Oops! It did at least conclude by suggesting thatFIELDis best discussed in conjunction withMETHODandCONSTRUCTORin #43, so maybe we'll take it back up then.(Do we have an issue yet for "Should we have a
@NullnessUnspecifiedannotation?" I know that we're figuring that we'll approach the remaining annotations more holistically, but we may want an issue, too, to at least start collecting history. [edit: filed #137])cpovirk commentedon Sep 28, 2020
As for
PARAMETERitself: I just left a related comment about Java's forthcoming "records" on #43.kevinb9n commentedon Jul 15, 2021
I would very much like to have a clear line between type-use annotations and scope annotations.
@NullMarkedshould just be a scope annotation (module, package, class, method, constructor).@Nullableshould be just a type-use annotation.What would it take for us to decide this?
cpovirk commentedon Jul 16, 2021
It seems moderately unlikely that we would support these scopes. (I'm now thinking that
TYPE_PARAMETERis particularly unlikely, but it looks like I've gone back and forth on this.)It seems overwhelmingly unlikely that we would support these scopes until we support
METHODandCONSTRUCTORscopes (#43).But we should at least consider
PARAMETERscope at the same time as we considerMETHODandCONSTRUCTORscopes, given the discussion of record components.Maybe that still lets us close this issue with the understanding that we may reopen it when we talk about #43? Again, I don't think anyone has been pushing for it to happen before then.