groovy icon indicating copy to clipboard operation
groovy copied to clipboard

Generate tuple constructor with all args and annotations

Open alien11689 opened this issue 8 years ago • 4 comments

alien11689 avatar Apr 10 '16 19:04 alien11689

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: https://github.com/apache/groovy/pull/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 avatar Apr 13 '16 10:04 paulk-asert

God damn it, I was just about to get fond of the Groovy syntax .... whoever came up with the "brilliant" idea of annotations should be hanged, drawn and quartered.

Looking at the homepage documentation, it actually seems like you can't even set a property to protected/private? But public works? I'm hoping I'm wrong, b/c that's just ... cancer.

I can't for the love of me understand how anybody could think it's a good idea. And yet this bullshit is spreading like locus between the programming dialects.

I managed to stay away with PHP for a while, but that language syntax is like a drove of old Java and C# dev's on a blind stampede, no idea what their doing except it should look like J/C#... </ end half-drunk rant>

bubach avatar Dec 15 '19 08:12 bubach

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 avatar Dec 15 '19 09:12 paulk-asert

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 avatar Aug 30 '22 05:08 paulk-asert