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

[jnigen] How to treat Java null? #1644

Open
HosseinYousefi opened this issue Oct 10, 2024 · 2 comments
Open

[jnigen] How to treat Java null? #1644

HosseinYousefi opened this issue Oct 10, 2024 · 2 comments

Comments

@HosseinYousefi
Copy link
Member

HosseinYousefi commented Oct 10, 2024

So far we decided not to make every Java object nullable in Dart because without additional annotations, every Java object can be nullable and the resulting code will be full of null-checks. However, we may even prefer this because then we can't forget to do if (nullableJObject.isNull) as the type system prevents us.

I recently made fromReference internal, so now the only way to pass null without hitting a lint is the verbose JObject.fromReference(jNullReference).as(Foo.type)!

  • We can make all methods accept Foo? and if null is passed, we interpret that as a Java object with a reference of nullptr.
    • If we generate all objects as nullable, then this happens automatically!
  • We can expose a jnull constant which users have to cast to the right object jnull.as(Foo.type)

wdyt @liamappelbe @dcharkes @stuartmorgan?

@stuartmorgan
Copy link

stuartmorgan commented Oct 10, 2024

without additional annotations, every Java object can be nullable and the resulting code will be full of null-checks

From what I've seen in our own plugin development, annotating the nullability of everything public is best pratice (for interop with Kotlin, I assume). Given that annotations to solve this problem exist and are readily available I would think we would want to optimize for making better type safety easier, rather than optimizing for the ease of use of code that's not annotated.

Also, without annotations, the correct interpretation in a language like Dart is (as you said) that everything can be nullable. If everything could be nullable, then code being full of null checks is correct. So that doesn't actually seem like a problem to me (in the case of non-annotated code that can't be annotated for whatever reason).

@HosseinYousefi
Copy link
Member Author

Sounds good. I can use the same annotations Kotlin uses to detect nullability by default: https://kotlinlang.org/docs/java-interop.html#nullability-annotations

and also add a way to include more annotations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

2 participants