Skip to content

Attributes

Attributes annotate methods, classes, fields, and properties. They map directly to .NET attributes in the compiled output.

@[AttributeName]
@[AttributeName(arg1, arg2)]
@[AttributeName(named_arg: value)]
@[HttpGet("/users")]
def index: Array[User]
end
@[Serializable]
class User
end
class User
@[Column("user_name")]
property name: String
@[NotMapped]
getter display_name: String
end

Stack on separate lines:

@[HttpGet]
@[Route("/users")]
def index: Array[User]
end

Custom attributes are classes that inherit from Attribute (an alias for System::Attribute). The class name must end in Attribute by convention — the suffix is optional when using @[...]:

class RouteAttribute < Attribute
property path: String
property method: Symbol = :get
def initialize(@path: String, method:: Symbol = :get)
@method = method
end
end
# Used without the Attribute suffix:
@[Route("/users", method: :post)]
def create: User
end

The compiler enforces that attribute classes:

  • Inherit from Attribute
  • End in Attribute
  • Only use types that are valid as .NET attribute arguments (primitives, String, Symbol, Type, and arrays of these)

.NET attributes cannot take a Symbol argument, so Symbol parameters and properties on attribute classes compile to String. A symbol passed to an attribute is stored as its name (:post → "post"), and Amethyst code reading the property gets a Symbol back. C# sees a string.


Attributes compile directly to .NET System.Attribute subclasses. Any .NET attribute can be used in Amethyst code, and Amethyst-defined attributes are usable from C#. The Attribute suffix is preserved in the compiled IL.