9: Attributes [✓] - royal-lang/rl GitHub Wiki

There are two types of attributes in Royal.

User-defined attributes and system attributes.

User-defined attributes are attributes that can/have been defined in code whereas system attributes are attributes already known by the compiler as it's a part of Bausslang's system.

Multiple attributes can be defined for each declaration.

Certain attributes are not tied to the next declaration but rather to the whole scope until a new attribute that negates it is defined.

Ex. immutable will make everything immutable until the mut attribute is defined.

User-defined Attributes

User-defined attributes are simply compile-time available structs. You set the attribute of a field, function etc. by declaring it before its declaration using the @ symbol.

@ATTRIBUTENAME(ATTRIBUTE_PARAMETERS):

Example:

struct Foo
{
    var int a;
    var string message;
}
@Foo(100, "Message"):
fn foo()
{
    // The @Foo() attribute is tied to the foo() function.
}

System Attributes

System attributes include access-modifiers and mutability attributes.

ATTRIBUTE:

List of attributes with scope effect:

  • public
  • private
  • protected
  • package
  • immutable
  • mut
  • const

List of attributes without scope effect: (Ties to the next declaration.)

  • unsafe
  • trusted
  • throwing
  • nostacktrace

List of attributes that can be specified to a type: (This can negate scope attributes as well.)

  • immutable
  • mut
  • const

Example:

immutable:

var int immutableInt1 = 100;

fn foo()
{
    var int immutableInt2 = 200;
}

mut:

fn foo2()
{
    var int mutableInt = 300;
}