dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
152 stars 42 forks source link

[jnigen] How to treat Java `null`? #1644

Open HosseinYousefi opened 1 week ago

HosseinYousefi commented 1 week ago

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)!

wdyt @liamappelbe @dcharkes @stuartmorgan?

stuartmorgan commented 1 week ago

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 commented 1 week ago

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.