typetools / typetools/checker-framework

documentation request: standard sample for Collection#toArray implementation

Open
#3,624 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

I'm a bit puzzled on the way to implement Collection#toAraray().

  protected static class Flat1List<T>
      extends AbstractFlatList<T>
      implements ComparableList<T> {
    private final T t0;

    public @PolyNull Object[] toArray(Flat1List<@PolyNull T> this) {
      Object[] res = new Object[1];
      res[0] = t0;
      return res;
    }

results in

FlatLists.java:398: error: [assignment.type.incompatible] incompatible types
in assignment.
      res[0] = t0;
               ^
  found   : T extends @Initialized @Nullable Object
  required: @Initialized @NonNull Object

    public @PolyNull Object[] toArray(Flat1List<@PolyNull T> this) {
      return new Object[]{t0};
    }

results in

FlatLists.java:397: error: [return.type.incompatible] incompatible types in r
eturn.
      return new Object[]{t0};
             ^
  type of expression: @Initialized @Nullable Object @Initialized @NonNull []
  method return type: @Initialized @PolyNull Object @Initialized @NonNull []

It looks like the workaround is to use, however, it is awkward. Am I doing something wrong?

    public @PolyNull Object[] toArray(Flat1List<@PolyNull T> this) {
      return new Object[]{castNonNullWithoutAssert(t0)};
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating Flat1List and its toArray implementation in FlatLists.java, then reproduce the two Checker Framework diagnostics shown in the issue. Review the Collection#toArray contract and the existing castNonNullWithoutAssert workaround. Done looks like documenting a standard sample implementation and clarifying whether that workaround is required.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.