heremaps / gluecodium

Cross-language bindings generator for C++, Java, Swift, and Dart
Apache License 2.0
202 stars 27 forks source link

Investigate possibility to init default field in place in Java #1294

Closed Hsilgos closed 2 years ago

Hsilgos commented 2 years ago

Currently fields with default parameters are set in generated constructor. It's desirable to investigate possibility to generate initialisation in place. Such approval had its own advantages like priority order of initialisation, optimisation and so on.

DanielKamkha commented 2 years ago

In-constructor field init is the only option for @Immutable structs. The reason is Java syntax: final fields with in-field default values cannot be (re)assigned with non-default values in the ctor body.

My guess is that mutable structs were made to have in-ctor init for consistency reasons.

So there is no alternative for @Immutable structs. For mutable ones we could create a new per-field attribute for enabling in-field init selectively.

DanielKamkha commented 2 years ago

Superseded by https://github.com/heremaps/gluecodium/issues/1295