Skip to content

Generate tuple constructor with all args and annotations#311

Closed
alien11689 wants to merge 1 commit intoapache:masterfrom
alien11689:generateTupleConstructorWithAllArgsAndAnnotations
Closed

Generate tuple constructor with all args and annotations#311
alien11689 wants to merge 1 commit intoapache:masterfrom
alien11689:generateTupleConstructorWithAllArgsAndAnnotations

Conversation

@alien11689
Copy link
Copy Markdown
Contributor

No description provided.

@paulk-asert
Copy link
Copy Markdown
Contributor

I like the idea but I think we can make it more generic. Perhaps something like what I show here is a desirable prerequisite tweak to Groovy:
#312

We could then apply the same idea to your PR to not just supply annotation class names but definitions including any needed annotation attributes?

@paulk-asert
Copy link
Copy Markdown
Contributor

Hi Christoffer, perhaps you can explain what you are trying to do and we can show you how to do it in Groovy.

The Java approach to properties is to write the getters, setters and backing field yourself. Groovy properties are a short-hand for the 99% common case of public getters/setters and private backing field but you can always write yourself as per Java for special cases or write your own meta-programming to give you other combinations. When writing yourself fields and methods can be private, protected, package-private or public.

Annotations aren't required but many developers like the magnitude productivity increase that compile-time meta-programming offers.

@paulk-asert
Copy link
Copy Markdown
Contributor

paulk-asert commented Aug 30, 2022

I know it doesn't cover all use cases, but for records in Groovy 4, annotations on the record are carried over to the constructor if placed on the record. It would cover the original use case:

import groovy.transform.Generated
import java.lang.annotation.*

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CONSTRUCTOR)
public @interface Example1 {}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CONSTRUCTOR)
public @interface Example2 {}

@Example1 @Example2
record Person(String firstName, String lastName) { }

assert Person.declaredConstructors[0].declaredAnnotations*.annotationType() == [Generated, Example1, Example2]

@paulk-asert
Copy link
Copy Markdown
Contributor

The idea is good but I don't think we'd implement it this way especially given the amount of time that has passed. I'll close this for now. If you still feel strongly about the desired goal, please create an issue and we can discuss an alternative implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants