mikhail.zarechenskiy
06/03/2025, 8:39 AMPerson(@Email val email: String)
, the @Email
annotation would be applied to both the constructor parameter and the field, while today it only applies to the parameter. This change seems safe and reasonable, and was originally motivated by KT-19289
At the same time, we’ve been considering a more aggressive default: applying annotations to all applicable targets. For instance, in @Anno var x: String
, the annotation could be applied to the property, backing field, getter, setter parameter, and so on. This is similar to how annotations work for records in Java.
Initially, we were cautious about this idea, we thought it might break some frameworks. But based on recent conversations, it seems this concern may not be as serious as we thought. Perhaps we should move toward this broader default after all. So I wonder if you could share your thoughts and any known pitfalls of changing the default.
What do you think?CLOVIS
06/03/2025, 8:56 AM@property:Anno
syntaxes?mikhail.zarechenskiy
06/03/2025, 8:59 AM@property:
efemoney
06/03/2025, 9:53 AMJavier
06/03/2025, 9:57 AMJavier
06/03/2025, 9:59 AMefemoney
06/03/2025, 10:02 AMmikhail.zarechenskiy
06/03/2025, 10:02 AMJavier
06/03/2025, 10:02 AMJavier
06/03/2025, 10:02 AMJavier
06/03/2025, 10:03 AMZac Sweers
06/03/2025, 10:26 AMZac Sweers
06/03/2025, 10:26 AMYoussef Shoaib [MOD]
06/03/2025, 10:53 AM@Anno var x: String
, the annotation won't be applied to the type, right? That's the only place where I can see issues. There's realistic scenarios I think where @Anno var x: String
and @Anno var x: @Anno String
mean different thingsmikhail.zarechenskiy
06/03/2025, 10:57 AMhfhbd
06/03/2025, 11:42 AMclass A(@Foo val s: String)
and class A(@property:Foo val s: String)
. It is easy to forgot the property:
prefix (because some plugins do not lookup all locations).mikhail.zarechenskiy
06/03/2025, 11:44 AMhfhbd
06/03/2025, 11:49 AMZac Sweers
06/03/2025, 12:01 PMRuckus
06/03/2025, 3:23 PM@JvmName("bob") var fred: String
would be roughly equivalent to this in Java:
String bob;
String bob() {
return this.bob;
}
void bob(String bob) {
this.bob = bob;
}
If that's the case, and there are multiple conflicting annotations, which one wins? For example:
@get:JvmName("getBob")
@JvmName("bob")
var fred: String
Does the latest one take precedence? Or perhaps the higher specificity?mikhail.zarechenskiy
06/03/2025, 4:40 PM@JvmName
doesn’t have the PROPERTY
target, so you still have to use `get`/`set` targets.
As for the more general case, I believe we should preserve the current behavior, so annotations with a use-site target take precedenceJP Sugarbroad
06/03/2025, 7:23 PM@PreferredTarget
annotation to the annotation class when necessary that way.