You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A single shorthand scheme for augmented types, for prose and other non-source-code contexts #129
kevin1e100 and I touched on this a bit in #32, and I think it's come up elsewhere. It's not a question that it's JSpecify job to answer, but we should keep it in mind as we discuss naming of annotations -- and maybe features that could add more information to augmented types (like having both lower and upper bounds?). Plus, we have many tool authors following these issues, and the GitHub issue UI is nice (and permits cross-linking with our other discussions). So I'm creating an issue here about the question. If nothing else, the issue may help me resist the urge to talk about this further on other issues :)
One way for tools to print augmented types (in their error messages, in tooltips in IDEs, etc.) is to make them look much like the source -- @Nullable Foo, @NullnessUnspecified T, etc. [edit: This is complicated by the existence of both declaration and type-use annotations, either or both of which may be in use in a given file.]
Another way is to use special symbols, similar to Kotlin -- Foo?, T!, etc.
We already gave some thoughts about symbols (especially !) in the linked discussion on issue 32. (And I owe kevin1e100 a reply, which I'll put below.)
Another thing to think about is that Java already uses ? for wildcards. This isn't ideal, but it doesn't have to be a fatal flaw: I don't think there's true ambiguity. (I do seem to recall that, the Checker Framework sometimes says things like @Nullable ?. Under Kotlin rules, that would become ??, which is no good. Perhaps we'd say (?)? or something.... But I expect most tools to refer to captured type variables instead thereby sidestepping the problem (by producing T1?, etc.).)
We can also consider whether we'll someday have non-nullness-related kinds of type annotations. I don't know that we will, but you can at least imagine having a type-use @CompileTimeConstant, @FormatString, @TypeCompatibleWith, something related to immutability and thread-safety?? Anyway, my point is that we might end up ouputting types using special symbols for nullness but using annotations for the other cases. That's probably tolerable, too.
@NullnessUnspecified is so noisy when it is never the cause of the failure
Thanks for observing this.
To me, ! suggests "not null"
That is in fact what it meant in Spec# I believe. I've at times used !?, FWIW, but am not claiming it's a great solution.
It may not be popular, but besides String!? there does seem to be another reasonable solution: just String could be used to mean @NullnessUnspecified String / String!? in error messages as long as both other options, @NonNull String / String!! and @Nullable String / String?, are always made explicit. This would remove "unspecified" clutter as desired and is consistent with the observation that
the token "@NullnessUnspecified" never appears in the source file I'm imagining.
I understand that this would take us away from Kotlin's error messages and is incompatible with the desire to generally write String!! as String. Instead, this solution would leverage "unspecified" as the default-default (so should still be reasonably intuitive to Java users).
Thanks for the reference to Spec#. I could probably go along with !?, among other options.
As for making String itself stand for @NullnessUnspecified String: My main concern is type-variable usages. We need a way to distinguish "T in annotated code" from "T in unannotated code." Using a more Kotlin-style notation, they're T and T!?, respectively. If we instead use T for the unannotated code, then "T in annotated code" probably has to become T!. That may sound like "T but not null," when in fact T might still permit nulls.
(Maybe this is solvable by replacing ! with a different symbol in that model. Avoiding ! may be nice, anyway, as you say, to avoid the Kotlin connection.)
I also feel a little sad that annotating more code for nullness may make error messages slightly longer (by adding ! in places). But I'm not too concerned about a single character, even when multiplied by n types.
And I do acknowledge the upside that using String for unspecified matches what users see today in Java -- and somewhat matches what they will continue to see in the various non-nullness-related error messages they get from javac.
I'm definitely in favor of this project developing a body of recommendations for tool authors in addition to our specifications.
And I think consensus guidelines about error/warning messages are a great fit for that.
And I heartily approve of concise notations for this (String? for @Nullable String). The notation must be universal regardless of whether the offending code is under @NullMarked or not, [so String must mean what it always has] [edit: nah].
I've realized that I want a good way to talk about nullness-augmented types in prose, and I think that need and this may be the same need.
We often do this in prose by writing out source code, but it's unwieldy. Plus technically we aren't bound by this convention, especially since what source code shows isn't really types anyway; it only shows declarations of types and usages of types, never types "themselves".
Optimistically, we can teach users that the source code @Nullable String represents a usage of a type that we write down in prose as String? and that we pronounce verbally as "string-or-null". That primes them to understand compiler messages once they start seeing them. IntelliJ probably already has an option to render @Nullable String that way in the editor as it is (?).
So then what notation for the other two kinds? I would suggest:
For unspecified, String*, pronounced "string-maybe-or-null" or "string-unspecified" (or "string-oblivious" to be like C#)
For non-null, String!, pronounced "string" because after all we should be saying everything with enthusiasm!
I think it at least makes sense to decorate all three kinds for these reasons
All three carry "specific knowledge", whereas an unannotated usage of a nullable-bounded type variable (UUONBTV?) has parametric nullness, which is like the lack of all specific knowledge, and that seems like the thing to show unadorned.
While it does seem like the fact that javac messages will show unadorned base types is an argument for letting unadorned mean unspecified, I'm not so sure. If nullness messages are always using one of String!/String*/String? then a message including just String will look more clearly like something that's just not trying to talk about nullness at all, which I think might be good.
changed the title [-]Discussion for tool authors: how to print augmented types?[/-][+]How shall we describe augmented types in prose, which tool owners may take after in their finding messages?[/+]on Apr 22, 2022
In error messages, I'd feel a little sad adding even one character for the non-null case (i.e., saying String! instead of String). One character is not a big deal, but it adds up for complex types. That goes not just for printing parameterized types individually but also for printing whole signatures, which may contain a number of types.
Also, the difference from Kotlin's meaning is unfortunate.
Neither of those is a deal-breaker on its own. But put them together, and I think we'd be better served by String in error messages, especially when users are likely to be switching between Java and Kotlin error messages as they work.
I think it would be OK for prose and error messages to differ in this respect: Prose needs to express "When you write Foo, you get Foo! or Foo*, depending on...." In contrast, error messages live in a world in which we've already converted the source code to types. It's true that users will also see compile errors that are unrelated to nullness, and there is definitely value in making those look distinct. But I think the greater danger is in making nullness error messages look scarier than they need to.
I would very very much like us to be able to settle on ONE scheme for how to notate these types. So I think we have some tension over whether non-null should be explicitly marked with !.
One complication is that we're supporting non-null projection of type variables (for the moment), meaning that T and T! have distinct meanings (unprojected T, and T projected to non-null).
I do think this is important to nail down and rely on in communication/documentation/etc.
The suffix that goes naturally with each of the 4 nullness descriptors in Chris's current spec are:
UNION_NULL "?"
MINUS_NULL "!"
NO_CHANGE ""
UNSPECIFIED "*"
For type variables these are all distinct and all make sense. (Well, maybe the last doesn't, but let's move on)
For non-variable reference types in null-marked code, "" and "!" are the same in effect. I propose we promote the explicit, redundant usage of the "!" suffix to provide needed clarity, in all contexts. This scheme will then work equally well for talking about null-unmarked code, where "no change" might have seemed to mean "unspecified".
I think the only case I left uncovered is value types (i.e. the 8 primitives) and suffixes should just not apply in that case.
So the proposal:
? suffix always means UNION_NULL and is always used for that
! suffix always means MINUS_NULL and is always used for that even when strictly unnecessary
* suffix always means UNSPECIFIED and is always used for that
No suffix always means NO_CHANGE and is only recommended for type variable usages and value types (for nonvariable reference types, use MINUS_NULL/! instead)
Also, as I think maybe David was the first to suggest, I think Foo!<Bar?> is much nicer than Foo<Bar?>!.
I think our reference checker should follow this scheme in its error messages. But if Tool X doesn't want to, okay. We could try out letting these actually show to our internal users and see how it goes.
I like this proposal generally. I'm a little worried about leaving out the ! for value types. Right now the primitive types are well known and spelled differently by convention (lowercase initial letter), so it's easy to remember that int doesn't include null. But in a Valhalla world there will be value types that are not well known and are spelled with an initial uppercase letter just like reference types, and I think those should use !.
What's wrong with always using int! for consistency? (Not disallowing int, but not preferring it either.)
...I think that it would be pretty clear that the method was returning a nullable value when a non-nullable value was expected.
Maybe there are cases in which ! would be more useful. But I think it's more likely to be a net negative, as laid out in #129 (comment). To expand slightly on that: It would be a nice carrot for error messages to become simpler as users specify the nullness of more types -- in particular when the alternative is for error messages to look more like Kotlin but with slightly different meaning!
I think we should consider specifications separately from tool outputs.
I think specifications, including these design discussions and conformance tests, should always prefer the most unambiguous terms, including int!.
On the other hand, tool outputs such as error messages can be freer to prefer shorter forms when they expect users to take context into account. In any case, we don't intend to constrain tool output very harshly.
I'd like not to care very much about tool outputs. I don't expect Kotlin to change how they refer to platform types (with !), for example, even though that directly conflicts with our notation here.
But in a Valhalla world there will be value types that are not well known and are spelled with an initial uppercase letter just like reference types,
News: I've been pushing hard that the value type always be spelled Foo.val (where Foo is the corresponding reference type), and at the moment I seem to be winning. But if it goes the other way, we'll have plenty of time to reconsider.
What's wrong with always using int! for consistency? (Not disallowing int, but not preferring it either.)
My feeling is that people would find it strange, distracting, and/or overzealous.
Another way to serve that goal is to always output augmented types inside special markers, at least when using them in prose or other locations that we worry may be ambiguous. So we could write that "The type of List<@Nullable String> in null-unmarked code is 'List*<String?>', but the type in null-marked code is 'List<String?>'."
Reminiscent of putting slashes around regular expressions when you mention them. (But, most people don't do that.)
My guess is that people would bother to do that rarely enough that no one would understand what it means when they do anyway. It also feels a wee bit like an "extra" detail to have to teach people besides just the idea of a suffix.
Issues
Issue 1: non-null character
IF a non-null character is needed, what should it be?
!
− means something else in kotlin
+
~ regex precedent argues for no suffix, but + is at least close-ish to the right meaning
Issue 2: parametric character
IF a parametric character is needed, what should it be?
%
+ starts with p; symbol could be seen as suggesting 'this or that'
I still find myself really reluctant to give this up in the current docs, so I'm going to quasi-optimisticallly retarget for 0.3, knowing we could still bump it back out again.
There's a detailed reasoned argument against putting the nullness signifier before the type arguments. Enough work was put into it that I'm reopening this for now, but the doc I'm referring to should be published. @eamonnmcmanus
Sorry, I wasn't able to share the original document publicly and I procrastinated about making a sharable copy. There is one now: Short-form nullness for generic types.
The JEP definitely changes how I feel about various notation questions (mainly because mirroring Java is much more important than mirroring Kotlin):
this post/mid discussion
the use of ! for non-nullable
what to do about * and % (and I still think one other that we'd need for a complete set), along with whether to use ! for non-type-variable types (e.g., String)
Parts of that last one are still tricky, since we have less idea what the JDK will do for generics in particular. Even with that, it makes sense to be as much in contact with the language developers as possible.
I'd still consider using something like `!!` to denote effectively
definitely-non-nullable types, regardless of how they appear in source
code. The idea is to use a symbol that _doesn't_ appear in source code,
specifically because there are multiple ways to express this in source.
E.g., in JSpecify alone, there are two ways (for non-variables). The JEP
also alludes to the possibility of multiple syntaxes being supported
eventually. As an added benefit this sidesteps the issue that Kotlin error
messages use `!` to denote platform types.
By a similar notion it might be useful to have an explicit marker for types
whose nullness is unspecified, rather than using raw type names for that.
This adds clarity compared to raw type names appearing in null-marked
scopes (and in Kotlin source).
Essentially what I'm suggesting is to use explicit markers to identify
effectively nullable, non-nullable, and unspecified-nullness
types, respectively, independent of how it's written in source, for
clarity. Once we do that we can omit markers on parametric variables, or
have a marker there as well, not sure. Yes, that's more symbols in
prose/text, but it makes the text very clear.
Activity
cpovirk commentedon Sep 28, 2020
from #32:
Thanks for the reference to Spec#. I could probably go along with
!?, among other options.As for making
Stringitself stand for@NullnessUnspecified String: My main concern is type-variable usages. We need a way to distinguish "Tin annotated code" from "Tin unannotated code." Using a more Kotlin-style notation, they'reTandT!?, respectively. If we instead useTfor the unannotated code, then "Tin annotated code" probably has to becomeT!. That may sound like "Tbut not null," when in factTmight still permit nulls.(Maybe this is solvable by replacing
!with a different symbol in that model. Avoiding!may be nice, anyway, as you say, to avoid the Kotlin connection.)I also feel a little sad that annotating more code for nullness may make error messages slightly longer (by adding
!in places). But I'm not too concerned about a single character, even when multiplied by n types.And I do acknowledge the upside that using
Stringfor unspecified matches what users see today in Java -- and somewhat matches what they will continue to see in the various non-nullness-related error messages they get from javac.kevinb9n commentedon Jun 17, 2021
I'm definitely in favor of this project developing a body of recommendations for tool authors in addition to our specifications.
And I think consensus guidelines about error/warning messages are a great fit for that.
And I heartily approve of concise notations for this (
String?for@Nullable String). The notation must be universal regardless of whether the offending code is under @NullMarked or not, [soStringmust mean what it always has] [edit: nah].kevinb9n commentedon Apr 22, 2022
I've realized that I want a good way to talk about nullness-augmented types in prose, and I think that need and this may be the same need.
We often do this in prose by writing out source code, but it's unwieldy. Plus technically we aren't bound by this convention, especially since what source code shows isn't really types anyway; it only shows declarations of types and usages of types, never types "themselves".
Optimistically, we can teach users that the source code
@Nullable Stringrepresents a usage of a type that we write down in prose asString?and that we pronounce verbally as "string-or-null". That primes them to understand compiler messages once they start seeing them. IntelliJ probably already has an option to render@Nullable Stringthat way in the editor as it is (?).So then what notation for the other two kinds? I would suggest:
String*, pronounced "string-maybe-or-null" or "string-unspecified" (or "string-oblivious" to be like C#)String!, pronounced "string" because after all we should be saying everything with enthusiasm!I think it at least makes sense to decorate all three kinds for these reasons
String!/String*/String?then a message including justStringwill look more clearly like something that's just not trying to talk about nullness at all, which I think might be good.[-]Discussion for tool authors: how to print augmented types?[/-][+]How shall we describe augmented types in prose, which tool owners may take after in their finding messages?[/+]cpovirk commentedon Apr 22, 2022
I think I'd be good with that in prose.
In error messages, I'd feel a little sad adding even one character for the non-null case (i.e., saying
String!instead ofString). One character is not a big deal, but it adds up for complex types. That goes not just for printing parameterized types individually but also for printing whole signatures, which may contain a number of types.Also, the difference from Kotlin's meaning is unfortunate.
Neither of those is a deal-breaker on its own. But put them together, and I think we'd be better served by
Stringin error messages, especially when users are likely to be switching between Java and Kotlin error messages as they work.I think it would be OK for prose and error messages to differ in this respect: Prose needs to express "When you write
Foo, you getFoo!orFoo*, depending on...." In contrast, error messages live in a world in which we've already converted the source code to types. It's true that users will also see compile errors that are unrelated to nullness, and there is definitely value in making those look distinct. But I think the greater danger is in making nullness error messages look scarier than they need to.kevinb9n commentedon Jun 21, 2022
I would very very much like us to be able to settle on ONE scheme for how to notate these types. So I think we have some tension over whether non-null should be explicitly marked with
!.One complication is that we're supporting non-null projection of type variables (for the moment), meaning that
TandT!have distinct meanings (unprojected T, and T projected to non-null).kevinb9n commentedon Jun 22, 2022
I do think this is important to nail down and rely on in communication/documentation/etc.
The suffix that goes naturally with each of the 4 nullness descriptors in Chris's current spec are:
"?""!""""*"For type variables these are all distinct and all make sense. (Well, maybe the last doesn't, but let's move on)
For non-variable reference types in null-marked code,
""and"!"are the same in effect. I propose we promote the explicit, redundant usage of the"!"suffix to provide needed clarity, in all contexts. This scheme will then work equally well for talking about null-unmarked code, where "no change" might have seemed to mean "unspecified".I think the only case I left uncovered is value types (i.e. the 8 primitives) and suffixes should just not apply in that case.
So the proposal:
?suffix always means UNION_NULL and is always used for that!suffix always means MINUS_NULL and is always used for that even when strictly unnecessary*suffix always means UNSPECIFIED and is always used for that!instead)Also, as I think maybe David was the first to suggest, I think
Foo!<Bar?>is much nicer thanFoo<Bar?>!.I think our reference checker should follow this scheme in its error messages. But if Tool X doesn't want to, okay. We could try out letting these actually show to our internal users and see how it goes.
netdpb commentedon Jun 22, 2022
I like this proposal generally. I'm a little worried about leaving out the
!for value types. Right now the primitive types are well known and spelled differently by convention (lowercase initial letter), so it's easy to remember thatintdoesn't include null. But in a Valhalla world there will be value types that are not well known and are spelled with an initial uppercase letter just like reference types, and I think those should use!.What's wrong with always using
int!for consistency? (Not disallowingint, but not preferring it either.)cpovirk commentedon Jun 22, 2022
In error messages in particular, do you think that the lack of
!would lead to confusion?For example, if a user saw this...
...I think that it would be pretty clear that the method was returning a nullable value when a non-nullable value was expected.
Maybe there are cases in which
!would be more useful. But I think it's more likely to be a net negative, as laid out in #129 (comment). To expand slightly on that: It would be a nice carrot for error messages to become simpler as users specify the nullness of more types -- in particular when the alternative is for error messages to look more like Kotlin but with slightly different meaning!netdpb commentedon Jun 22, 2022
I think we should consider specifications separately from tool outputs.
I think specifications, including these design discussions and conformance tests, should always prefer the most unambiguous terms, including
int!.On the other hand, tool outputs such as error messages can be freer to prefer shorter forms when they expect users to take context into account. In any case, we don't intend to constrain tool output very harshly.
I'd like not to care very much about tool outputs. I don't expect Kotlin to change how they refer to platform types (with
!), for example, even though that directly conflicts with our notation here.kevinb9n commentedon Jun 22, 2022
News: I've been pushing hard that the value type always be spelled
Foo.val(whereFoois the corresponding reference type), and at the moment I seem to be winning. But if it goes the other way, we'll have plenty of time to reconsider.My feeling is that people would find it strange, distracting, and/or overzealous.
31 remaining items
kevinb9n commentedon Sep 22, 2022
Wiki page for this notation exists now. Incomplete, but spells out the goals I think we're shooting for.
Reminiscent of putting slashes around regular expressions when you mention them. (But, most people don't do that.)
My guess is that people would bother to do that rarely enough that no one would understand what it means when they do anyway. It also feels a wee bit like an "extra" detail to have to teach people besides just the idea of a suffix.
Issues
Issue 1: non-null character
IF a non-null character is needed, what should it be?
!++is at least close-ish to the right meaningIssue 2: parametric character
IF a parametric character is needed, what should it be?
%Issue 3: Scheme
Comparison of options
For the table below I'll just arbitrarily assume
+and%from above.Example signature:
class Foo<A extends @Nullable Object, B>A@NonNull AB/@NonNull BXy/@NonNull Xykevinb9n commentedon Oct 14, 2022
I still find myself really reluctant to give this up in the current docs, so I'm going to quasi-optimisticallly retarget for 0.3, knowing we could still bump it back out again.
kevinb9n commentedon Oct 26, 2022
To get a "working decision" for now, I claim the simplest thing we can do is:
!for NN because that's what we've been doingI can see a likely enough change to
+or to a different scheme before 1.0.For now we are marking issues closed when they have a working decision.
kevinb9n commentedon Mar 13, 2024
There's a detailed reasoned argument against putting the nullness signifier before the type arguments. Enough work was put into it that I'm reopening this for now, but the doc I'm referring to should be published. @eamonnmcmanus
eamonnmcmanus commentedon Aug 2, 2024
Sorry, I wasn't able to share the original document publicly and I procrastinated about making a sharable copy. There is one now: Short-form nullness for generic types.
kevinb9n commentedon Aug 15, 2024
Now that https://openjdk.org/jeps/8303099 exists, I think the nature of this debate changes. JSpecify should simply mirror what the language syntax is going to be -- to the extent we can know/predict it.
Éamonn and I should probably repurpose this debate into a debate about that syntax, in that forum.
cpovirk commentedon Aug 16, 2024
The JEP definitely changes how I feel about various notation questions (mainly because mirroring Java is much more important than mirroring Kotlin):
!for non-nullable*and%(and I still think one other that we'd need for a complete set), along with whether to use!for non-type-variable types (e.g.,String)Parts of that last one are still tricky, since we have less idea what the JDK will do for generics in particular. Even with that, it makes sense to be as much in contact with the language developers as possible.
kevin1e100 commentedon Aug 16, 2024
kevinb9n commentedon Aug 16, 2024
Aside from !/!! that sounds like the current scheme, which is documented at https://github.com/jspecify/jspecify/wiki/notation
This issue is open to consider changes to that scheme:
String?[]![]?wrong in that