Skip to content

Annotations on function parameters #1021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
emdemir opened this issue Mar 12, 2025 · 2 comments
Closed

Annotations on function parameters #1021

emdemir opened this issue Mar 12, 2025 · 2 comments

Comments

@emdemir
Copy link

emdemir commented Mar 12, 2025

Problem Definition

I'd like to expose metadata on function parameters through reflection, which can later be used for things like code generators. An example could be defining HTTP APIs using abstract functions which could look like this:

@http.GET
@http.Path("/{name}")
abstract function getUserInfo(@http.FromPath name: String): UserInfo

I tried to see if there are other approaches such as defining this in constraints, but those aren't accessible through pkl:reflect.

Solution

Make it possible to add annotations to function parameters.

Alternatives

  • Add the annotation to the function itself with something like @http.FromPath("location"). Makes it repetitive and moves the annotation away from its target.
  • Make the function arguments a class and define the annotations on properties. It becomes much more verbose to define things.
@HT154
Copy link
Contributor

HT154 commented Mar 12, 2025

Using a class here actually seems idiomatic to me as a means of declaratively defining inputs and outputs. Functions in Pkl are limited (intentionally). It's actually common to use classes as functions, so much so that there's a named pattern for doing so: Class-as-a-function. I get that it's a good deal more verbose, but I'd argue that's a good thing: in this example it seems like you'd want to be able to add a doc comment to each parameter explaining its purpose/functionality.

@emdemir
Copy link
Author

emdemir commented Mar 12, 2025

I see. And you have guessed correctly that being able to add documentation to parameters was my next problem. Then we will stick with a pattern similar to Class-as-a-function for the input. Thanks!

@emdemir emdemir closed this as completed Mar 12, 2025
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

No branches or pull requests

2 participants