native icon indicating copy to clipboard operation
native copied to clipboard

☂️ Idiomatic conversions

Open HosseinYousefi opened this issue 2 years ago • 3 comments

  • [ ] T getSomeValue() that returns a private value T someValue with the same type and doesn't get any arguments can be converted to a Dart's getter T get someValue – potentially only under a config flag
  • [ ] void setSomeValue(T t) that gets a value of type T and returns void can be converted into a Dart's setter set someValue(T t) – potentially only under a config flag
  • [ ] get methods that only get a single argument can be converted to operator [] – potentially only under a config flag
  • [ ] put methods that only get two arguments can also be converted to operator []=. We still probably want to generate put because put usually returns the value in Java while []= returns void – potentially only under a config flag
  • [x] equals method can overload operator == and hashCode can override the hash code getter.

More conversions that can make the code more idiomatic to Dart? Feel free to suggest.

HosseinYousefi avatar Jun 21 '23 11:06 HosseinYousefi

  • Boolean getters will be often named isField.

Overall this is similar to what most java serialization libraries do with getters / setters. (JSON, ORMs & DB drivers). They try to convert getters and setters into fields / columns.

I actually have a locally drafted proposal about better supporting such PoD (plain old data) types, but I think we are too early in the roadmap to discuss that feature.

mahesh-hegde avatar Jun 21 '23 18:06 mahesh-hegde

Big +1 for doing the first two!

That would be similar to what Kotlin to Java interop does: https://kotlinlang.org/docs/java-interop.html#getters-and-setters

mit-mit avatar Jan 25 '24 11:01 mit-mit

Big +1 for doing the first two!

That would be similar to what Kotlin to Java interop does: https://kotlinlang.org/docs/java-interop.html#getters-and-setters

Kotlin is doing some other conversions for operators as well but the documentation is somewhat vague about it: https://kotlinlang.org/docs/java-interop.html#operators

For example list[i] turns into list.get(i), and so on... (Similar to the other proposed conversions in this issue).

HosseinYousefi avatar Jan 25 '24 11:01 HosseinYousefi