Comments (4)
Unexpected behavior
If you uncomment the two lines in the original code to get the collapsed parameter+property syntax, both prop1 and prop2 have the same annotation, so as part of the collapsing process, the use-site target is lost
It doesn't look though that the use-site target got lost in these examples, as it has never been specified explicitly? I don't think that's a correct expectation.
To match the built type fully, we would expect prop1 to have @get:Suppress and prop2 to have @param:Suppress.
Not unless these use-site targets were set explicitly, the default is no use-site targets.
And to answer the questions:
what if the user specifies a use-site target on the AnnotationSpec itself, who should win
I'd say the user should be responsible for making sure there are no clashes, the library simply merges the annotations from the property and the parameter.
what if they specify an incorrect use-site target
That's user error, I don't think the library should be correcting that error, as the library doesn't know what the user's real intent was. Also, please note that it's perfectly possible to generate incorrect, uncompilable code with KotlinPoet. KotlinPoet very deliberately doesn't act as a Kotlin compiler (even though it packages some basic correctness checks), this is not its purpose.
If I could build a primary constructor with only PropertySpec's but not ParameterSpec's, it would be easier to figure out what the user really wanted, and the builder code would be less repetitive as well.
But this is not how Kotlin models these concepts. Primary constructor syntax allows you to simultaneously declare a property and a parameter with the same name, but they're still two different things, which KotlinPoet reflects.
All in all, it's not really clear what the issue here is. Is the ask to have the library auto-generate use-site targets based on which specs the annotations are attached to? Or is the ask to have more complex rules around merging annotations from properties and parameters?
from kotlinpoet.
I'm gonna close this issue as it's been inactive for over two weeks and it's not clear what the request is, but please feel free to reopen and clarify your expectations. Thank you!
from kotlinpoet.
I missed the notification.
All I'm saying is: if I annotate a property, it has a default use-site target of @get
, and if I annotate a parameter, it has a default use-site target of @param
.
You wrote:
Primary constructor syntax allows you to simultaneously declare a property and a parameter with the same name, but they're still two different things, which KotlinPoet reflects.
So I guess we're on the same page so far?
Then, when I build a constructor where the property and parameter are merged, and KotlinPoet omits the use-site target, then the annotation which was originally meant for the parameter, it also gets copied to the property, or vice versa.
What KotlinPoet does: if there was no use-site target on the originally built elements, then there's no use-site target on the final code output either. That is reasonable as well. The issue is that the lack of a use-site target has different meanings, because different contexts have different defaults. From that point of view, omitting the use-site target means adding a use-site target to an element where it was not specified.
from kotlinpoet.
Got it, I see what you're saying, thanks for clarifying! I think it would make sense to generate use-site targets to preserve behaviour.
from kotlinpoet.
Related Issues (20)
- Chained constructor call is not formatted correctly
- Move Gradle Wrapper validation to its own action
- Can't generate @get:JvmName annotation for property HOT 5
- Can we add IDE highlighting to addStatement or similar statements HOT 4
- kotlin.* imports in generated classes HOT 1
- There is no way to add format strings to the code HOT 2
- Support at least few Kotlin API targets via apiVersion to simplify migrations
- Split the KotlinPoet documentation page into multiple subpages
- addKdoc not working as expected when property is involved HOT 2
- com.squareup:kotlinpoet 1.15.3 missing class files. HOT 3
- callSuperConstructor does not generate code for its parameters HOT 2
- Code generated with ksp interop's toAnnotationSpec() generates code that fails to compile when passed an OptIn annotation HOT 1
- Integration with okio's file system HOT 4
- Aliased imports not working for nested classes and generate unused imports and usage of FQNs HOT 2
- Aliased imports not used when generating code in same package for which the aliased import is defined HOT 1
- Reference Kotlinpoet created class in another class.
- Kotlin Keywords escaped in Class and Property names HOT 6
- Comments outside of code block HOT 1
- Wrap supertype/superinterfaces like parameters
- The get / set index access operator overloads are incorrectly escaped HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlinpoet.