Skip to content

Commit 817e433

Browse files
authored
Document annotations and annotation converters (#1427)
1 parent 08d8c8e commit 817e433

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

docs/modules/language-reference/pages/index.adoc

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2736,6 +2736,10 @@ output {
27362736

27372737
For more on path-based converters, see {uri-stdlib-PcfRenderer-converters}[PcfRenderer.converters].
27382738

2739+
Special <<annotations,Annotations>> may also be used to influence how class properties are rendered.
2740+
The `ConvertProperty` annotation (and subclasses of it) are automatically applied during.
2741+
For more on annotation-based converters, see link:{uri-stdlib-ConvertProperty}[ConvertProperty].
2742+
27392743
Sometimes it is useful to directly compute the final module output, bypassing `output.value` and `output.converters`.
27402744
To do so, set the link:{uri-stdlib-baseModule}/ModuleOutput#text[output.text] property to a String value:
27412745

@@ -3203,6 +3207,7 @@ This section discusses language features that are generally more relevant to tem
32033207
<<module-keyword,`module` Keyword>> +
32043208
<<glob-patterns,Glob Patterns>> +
32053209
<<doc-comments,Doc Comments>> +
3210+
<<annotations,Annotations>> +
32063211
<<name-resolution,Name Resolution>> +
32073212
<<reserved-keywords,Reserved Keywords>> +
32083213
<<blank-identifiers,Blank Identifiers>> +
@@ -5332,7 +5337,7 @@ Module-level members can be prefixed with `module.` to resolve name conflicts:
53325337
/// See [module.pigeon].
53335338
----
53345339

5335-
To exclude a member from documentation and code completion, annotate it with `@Unlisted`:
5340+
To exclude a member from documentation and code completion, <<annotations,annotate>> it with `@Unlisted`:
53365341

53375342
[source%parsed,{pkl}]
53385343
----
@@ -5349,6 +5354,47 @@ The following member links are marked up as code but not rendered as links:footn
53495354

53505355
Nevertheless, it is a good practice to use member links in the above cases.
53515356

5357+
[[annotations]]
5358+
=== Annotations
5359+
5360+
Annotations are a mechanism to provides extra metadata about Pkl <<modules,modules>>, <<classes,classes>>, <<methods,methods>>, and <<properties,properties>>.
5361+
They provide metadata about the type or member they annotate that can be via link:{uri-stdlib-reflectModule}[reflection] or Pkl's Java APIs.
5362+
The most common use cases for annotations are to add metadata to influence behavior of xref:pkl-doc:index.adoc[Pkldoc], code generation tools, or <<module-output,module output>>.
5363+
5364+
Annotations are regular Pkl objects whose class extends link:{uri-stdlib-Annotation}[`Annotation`].
5365+
Annotation instances are defined similarly to regular <<classes, class instances>>, but instead of using the `new` keyword the class name is prefixed with `@`.
5366+
The object body may be omitted if an annotation's class has no properties or the declared annotation does not override any of the class's default values
5367+
Multiple annotations may be defined on a member.
5368+
If the annotated member has a <<doc-comments,doc comment>>, the annotation is defined between the comment and the member.
5369+
5370+
[source%tested,{pkl}]
5371+
----
5372+
/// Module doc comment
5373+
@SomeAnnotation // <1>
5374+
module myModule
5375+
5376+
class SomeAnnotation extends Annotation { // <2>
5377+
description: String = "some annotation"
5378+
}
5379+
5380+
/// Module doc comment
5381+
@SomeAnnotation // <3>
5382+
class Bird {
5383+
@SomeAnnotation { description = "some property" } // <4>
5384+
name: String
5385+
5386+
@SomeAnnotation { description = "some method" } // <5>
5387+
@Unlisted // <6>
5388+
function greet(greeting: String): String = "\(greeting), \(name)!"
5389+
}
5390+
----
5391+
<1> An annotation applied to a module. The annotation(s) must precede the `module` clause and follow the doc comment. The object body is omitted.
5392+
<2> The definition of an annotation class.
5393+
<3> An annotation appied to a class. The object body is omitted.
5394+
<4> An annotation applied to a property. The object body is included because the `description` property is overridden.
5395+
<5> An annotation applied to a method. The object body is included because the `description` property is overridden.
5396+
<6> A second annotation applied to the same method.
5397+
53525398
[[name-resolution]]
53535399
=== Name Resolution
53545400

0 commit comments

Comments
 (0)