kotlin icon indicating copy to clipboard operation
kotlin copied to clipboard

KAPT: Omit property initializers for non const properties in stubs

Open ansman opened this issue 3 years ago • 2 comments

The values might be read as compile time constants when they are in fact not.

This fixes KT-52410.

ansman avatar Sep 14 '22 16:09 ansman

I have not be able to identity any issues with omitting the initializer in the stub even though javac could theoretically complain since they need a value assigned.

If this is a concern there are two other solutions:

  1. Omit the initializer at the declaration site and move it to the constructor or an initializer block.
  2. Wrap primitive values in Boolean.valueOf, Integer.valueOf etc.
  • This solution still needs a solution for String? properties that are set to null.

I'd also like to discuss whether properties that are not const but still have a known fixed value at compile time (val foo = 1 for example) should have their values omitted or not. With the initial implementation all non const properties omit the initializer.

ansman avatar Sep 14 '22 16:09 ansman

Ok, I feel pretty happy about this now. Now only constructor parameters are affected and only primitives/strings.

There is a special case for interfaces as it seems that static interface fields require an initializer. We could perhaps use the wrapper approach for those.

ansman avatar Sep 15 '22 20:09 ansman