Attributes
Attributes annotate methods, classes, fields, and properties. They map directly to .NET attributes in the compiled output.
Syntax
Section titled “Syntax”@[AttributeName]@[AttributeName(arg1, arg2)]@[AttributeName(named_arg: value)]On methods
Section titled “On methods”@[HttpGet("/users")]def index: Array[User]endOn classes
Section titled “On classes”@[Serializable]class UserendOn properties and fields
Section titled “On properties and fields”class User @[Column("user_name")] property name: String
@[NotMapped] getter display_name: StringendMultiple attributes
Section titled “Multiple attributes”Stack on separate lines:
@[HttpGet]@[Route("/users")]def index: Array[User]endDefining Custom Attributes
Section titled “Defining Custom Attributes”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 endend
# Used without the Attribute suffix:@[Route("/users", method: :post)]def create: UserendThe 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.
.NET Interop
Section titled “.NET Interop”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.