<@Nullable T> vs <T extends @Nullable Object>

3 views
Skip to first unread message

Piotr Zielinski

unread,
Nov 16, 2009, 11:44:02 AM11/16/09
to Type annotations (JSR 308) discussions
Hi,

I'd like to have a template parameter that can accept nullable
objects. I've noticed that both constructs below work:

public static <T extends /*@Nullable*/ Object> T identity(T object)
{ return object; } // works
public static </*@Nullable*/ T> T identity(T object) { return
object; } // works

Is the latter officially supported (the manual only mentions the
former).

Also, for classes, only the former construct works, could somebody
comment on this?

class Test<T extends /*@Nullable*/ Object> {} // works
class Test</*@Nullable*/ T> {} // doesn't work

Thanks,
Piotr

Michael Ernst

unread,
Nov 16, 2009, 4:31:44 PM11/16/09
to jsr308-...@googlegroups.com, piotr.z...@gmail.com
Piotr-

Thanks for your question.

I'd like to differentiate the syntax that is permitted by JSR 308 (Java's
type annotations syntax) and the subset of that syntax that is used by the
Checker Framework. I'm not positive which of these you are referring to.

> public static <T extends /*@Nullable*/ Object> T identity(T object)
> { return object; } // works
> public static </*@Nullable*/ T> T identity(T object) { return
> object; } // works
>
> Is the latter officially supported (the manual only mentions the
> former).

These are both permitted by JSR 308.

Only the latter is officially supported by the Checker Framework (I guess
that's the manual you are referring to).

Someone could build a tool that uses the latter, or could enhance the
Checker Framework to use the latter.

> Also, for classes, only the former construct works, could somebody
> comment on this?
>
> class Test<T extends /*@Nullable*/ Object> {} // works
> class Test</*@Nullable*/ T> {} // doesn't work

Our rationale was that having two distinct syntaxes for the same concept
could be confusing to users, and the former more closely corresponds to the
type-checker's rules. (Or, do you want the two syntaxes to mean different
things?)

-Mike


PS: If the discussion is about the Checker Framework, then let's move
followups to checker-fram...@googlegroups.com .

Reply all
Reply to author
Forward
0 new messages