In Pony, we provide a special syntax for implementation-specific annotations to various elements of a program. The basic syntax is a comma-separated list of identifiers surrounded by backslashes:

  1. \annotation1, annotation2\

Here, annotation1 and annotation2 can be any valid Pony identifier, i.e. a sequence of alphanumeric characters starting with a letter or an underscore.

What can be annotated

Annotations are allowed after any scoping keyword or symbol. The full list is:

  • actor
  • class
  • struct
  • primitive
  • trait
  • interface
  • new
  • fun
  • be
  • if (only as a condition, not as a guard)
  • ifdef
  • elseif
  • else
  • while
  • repeat
  • until
  • for
  • match
  • | (only as a case in a match expression)
  • recover
  • object
  • { (only as a lambda)
  • with
  • try
  • then (only when part of a try block)

The effect of annotations

Annotations are entirely implementation-specific. In other words, the Pony compiler (or any other tool that processes Pony programs) is free to take any action for any annotation that it encounters, including not doing anything at all. Annotations starting with ponyint are reserved by the compiler for internal use and shouldn’t be used by external tools.

Annotations in the Pony compiler

The following annotations are recognised by the Pony compiler. Note that the Pony compiler will ignore annotations that it doesn’t recognise, as well as the annotations described here if they’re encountered in an unexpected place.

packed

Recognised on a a struct declaration. Removes padding in the associated struct, making it ABI-compatible with a packed C structure with compatible members (declared with the __attribute__((packed)) extension or the #pragma pack preprocessor directive in many C compilers).

  1. struct \packed\ MyPackedStruct
  2. var x: U8
  3. var y: U32

likely and unlikely

Recognised on a conditional expression (if, while, until and | (as a pattern matching case)). Gives optimisation hints to the compiler on the likelihood of a given conditional expression.

  1. if \likely\ cond then
  2. foo
  3. end
  4. while \unlikely\ cond then
  5. bar
  6. end
  7. repeat
  8. baz
  9. until \likely\ cond end
  10. match obj
  11. | \likely\ expr => foo
  12. | \unlikely\ let capt: T => bar
  13. end