mirror of
https://github.com/apple/pkl.git
synced 2026-01-11 22:30:54 +01:00
Annotations on function parameters #299
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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!