Skip to content

73 Runtime annotation metadata ​

A runtime-retained Label can be read from Task.class. The lookup returns a nullable Label?, so the sample uses safe access and a fallback.

norm
import std.annotation.RuntimeRetention
import std.annotation.TypeTarget

annotation Label implements TypeTarget, RuntimeRetention {
  String text
}

@Label(text: "task")
class Task {}

main() {
  Label? label = Task.class.annotation<Label>()
  printLine(label?.text ?? "missing")
}

Expected output:

text
task

Try it: Remove @Label and observe the fallback.

Precise rules: Reference.

Norm 0.25