Programming Languages - Annotations

Last Updated: 2024-01-13

Annotations: additional / meta information

  • for human: documentation
  • for compilers: optimization

C++: Attributes

Since C++11.

Syntax: [[attribute-list]], where attribute-list is a comma-separated sequence of zero or more attributes.

For example:

void Foo(int);

Java: Annotations

The at sign character (@) indicates to the compiler that what follows is an annotation.


  • @Override: it tells the compiler that the child class method is overwriting its base class method.
  • @SuppressWarnings("unchecked"): it tells the compiler to ignore the specified warning.

To declare an annotation:

public @interface Foo {
  public String bar();

Kotlin: Annotations

annotation class Fancy

Rust: Attributes

(Examples from the official site: https://doc.rust-lang.org/reference/attributes.html)

InnerAttribute: #![ Attr ]

  • if at the top level, it applies to the enclosing module or crate.
  • if inside a fn, it applies to the entire function.
// General metadata applied to the enclosing module or crate.
#![crate_type = "lib"]

OuterAttribute: #[ Attr ]: applies to the thing that follows the attribute.

fn test_foo() {
    /* ... */

Go: Struct Tags

Golang does not provide annotations, but you can use struct tags to store metadata.

type User struct {
	Name      string  `json:"name"`
	Password  string  `json:"password"`