Feature Request: @PostConstructor
## Proposal Allow a method to be invoked after `@<X>ArgsConstructor` Many times, we want to avoid the tedium of writing constructors, but still do some initialization or computation on non-final / non-injected fields. Right now, the only option is to use Lazy getters, but that is cumbersome and not safe because the computation happens at a different lifetime. A `PostConstructor` would greatly help reduce the pain of boilerplate while still doing something useful (like validation, precomputation, caching), and also catching initialization errors early instead of using a Lazy getter. ## Possible Implementations ### `@PostConstructor` method annotation ``` @<X>ArgsConstructor public class SomeClass { private final int var1; private final int var2; private int computedVar3; @PostConstructor private void someInitMethod() { this.computedVar3 = var1 + var2; } } ``` ### `postConstructor` annotation attribute (like `onConstructor` attribute on XArgsConstructor annotations) ``` @<X>ArgsConstructor(postConstructor="someInitMethod") public class SomeClass { private final int var1; private final int var2; private int computedVar3; private void someInitMethod() { this.computedVar3 = var1 + var2; } } ``` ### Expected output ``` public class SomeClass { private final int var1; private final int var2; private int computedVar3; public SomeClass(int var1, int var2) { this.var1 = var1; this.var2 = var2; this.someLazyInitMethod(); }
Posted by rdsubhas on Oct 7, 2016, 4:08 PM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
Feature Request: @PostConstructor
## Proposal Allow a method to be invoked after `@<X>ArgsConstructor` Many times, we want to avoid the tedium of writing constructors, but still do some initialization or computation on non-final / non-injected fields. Right now, the only option is to use Lazy getters, but that is cumbersome and not safe because the computation happens at a different lifetime. A `PostConstructor` would greatly help reduce the pain of boilerplate while still doing something useful (like validation, precomputation, caching), and also catching initialization errors early instead of using a Lazy getter. ## Possible Implementations ### `@PostConstructor` method annotation ``` @<X>ArgsConstructor public class SomeClass { private final int var1; private final int var2; private int computedVar3; @PostConstructor private void someInitMethod() { this.computedVar3 = var1 + var2; } } ``` ### `postConstructor` annotation attribute (like `onConstructor` attribute on XArgsConstructor annotations) ``` @<X>ArgsConstructor(postConstructor="someInitMethod") public class SomeClass { private final int var1; private final int var2; private int computedVar3; private void someInitMethod() { this.computedVar3 = var1 + var2; } } ``` ### Expected output ``` public class SomeClass { private final int var1; private final int var2; private int computedVar3; public SomeClass(int var1, int var2) { this.var1 = var1; this.var2 = var2; this.someLazyInitMethod(); }
Comments (0)
No comments yet. Start the discussion.