Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for guidance, allowance of nonnull in nullable, suggested path forward? #6380

Closed
spacether opened this issue Dec 21, 2023 · 1 comment

Comments

@spacether
Copy link

spacether commented Dec 21, 2023

Thanks for submitting an issue.
As explained in the instructions for submitting an issue at https://checkerframework.org/manual/#reporting-bugs, please include four pieces of information:

  • commands (that can be cut-and-pasted into a command shell),
  • inputs,
  • outputs, and
  • expectation.

So I have this data structure:
Nullable Map<String, Nullable Map<String, Nullable String>>
There are 3 nullable locations
But one can also pas non-null data into these locations, so one needs to write nonnull versions also like:
Map<String, Map<String, String>>
But these are combinations, so the number of method signatures here is factorial, which is not great.

The context here is that I am ingesting json data with a known nullable shape, and am instantiating output classes containing that data this PR.

Is there a way for me to write fewer than factorial methods here which will cover all use cases?
Casting NonNull into Nullable would allow me to just use the the signature.

Is this covariance?
Also is there an active slack/discord/irc community for this tool?
I would prefer to ask there but only saw the mailing list mentioned as an option.
Apologies if this is the wrong place to ask this.

My real world example:
With this code invocation:

        schema.validate(
            MapMaker.makeMap(
                new AbstractMap.SimpleEntry<>(
                    "foo",
                    "baz"
                ),
                new AbstractMap.SimpleEntry<>(
                    "bar",
                    2
                )
            ),
            configuration
        );

I get this error:

java: [argument] incompatible argument for parameter arg of validate.
  found   : @Initialized @NonNull Map<@Initialized @NonNull String, @Initialized @NonNull Object>
  required: @Initialized @NonNull Map<@Initialized @NonNull String, @Initialized @Nullable Object>

And the validate method is defined as:
public FrozenMap<@Nullable Object> validate(Map<String, @Nullable Object> arg, SchemaConfiguration configuration) throws ValidationException, InvalidTypeException {
It is also worth noting that these maps will be read from only, so perhaps @Covariant on an interface method is what I should be using here?

The ingested maps are only ever being read by the inner code, they are not being modified.

@spacether spacether changed the title Request for guidance, allowwance of nonnull in null, suggested path forward? Request for guidance, allowance of nonnull in null, suggested path forward? Dec 21, 2023
@spacether spacether changed the title Request for guidance, allowance of nonnull in null, suggested path forward? Request for guidance, allowance of nonnull in nullable, suggested path forward? Dec 21, 2023
@spacether
Copy link
Author

spacether commented Dec 22, 2023

Never mind, I was able to make this work by using a covariant type like:
? extends @Nullable Map<String, ? extends Nullable Map<String, ? extends Nullable String>>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant