Originally created by @emdemir on GitHub (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.
Originally created by @emdemir on GitHub (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 function`s which could look like this:
```pkl
@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.
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.
@HT154 commented on GitHub (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](https://pkl-lang.org/blog/class-as-a-function.html). 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.
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 commented on GitHub (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!
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @emdemir on GitHub (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: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
@http.FromPath("location"). Makes it repetitive and moves the annotation away from its target.@HT154 commented on GitHub (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 commented on GitHub (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!