Skip to content

Annotations

Vihan edited this page Dec 11, 2018 · 3 revisions

Annotations are little 'tags' you can put on functions, classes, variables, and other declarations to tell the compiler a little more about that declaration. You don't see annotations in everyday code, generally they offer more advanced options for how the code is compiled.

Syntax

Annotations immediately precede the declaration and may or may not have associated arguments. Annotations can have identifiers, strings, numbers, and booleans as arguments.

@Identifier
@Identifier("string", 123, true, value, ...)

Examples

Here is an example of a function inlined using the @inline annotation:

@inline
func add(x: Int, to y: Int) -> Int {
    return x + y
}

Here is an example of a class used for IO using the @dynamic annotation:

@dynamic(false)
class DataPacket {
    let header: UInt8
    let length: UInt32
    let body: ByteSequence
}

List of annotations

The following is an exhaustive list of all annotations.

Common Annotations

Name Arguments Decl Type Description
@backend identifier Function, Class This specifies that a given class or function should only be visible to a certain backend. This allows for backend-specific code. An example is a function declared @backend(js) func f() { ... } would be invisible to the LLVM compiler.
@dynamic boolean Class This specifies if a class should or should not engage in dynamic dispatch. This is useful when you must ensure a specific bit-layout or ABI.
@inline always Function Specifies a function should be inlined. This probably won't work with recursive functions. Useful applications are operators. If the always parameter is passed then the function will be forced to be inlined.
@primitive String | ByteSequence | Boolean | Integer | FloatingPoint | Regex Class This specifies a given literal can be used with the class. For example specifying @primitive(Integer) would mean that let a: T = 1 would work. This internally bitcasts the type to the target type.

Internal Annotations

These annotations are for internal use and may be removed or modified at any time:

Name Arguments Decl Type Description
@mock see below Class This specifies that a type should use a compiler-primitive. See below for the possible values this can take
@booleanProvider none Class Only one type per compilation index should have this annotation. Specifies boolean as the type expected in boolean-based items e.g. if
@staticEnumProvider none Class Specifies which type static enumerations should be internally backed by. Must be an internal type and only one per compilation index.
@manifestAsRoot none Class Specifies which class should act as the 'root' class. All contents are ignored merely the identifier and scope are noted. Only one per compilation index

Mock Type Values (LLVM)

Pointers
Name C-equivalent
pointer T*
opaquepointer void*
pointer8 char*
Boolean

i1

Integer-Related
Name Signed Size (bits)
ui8 false 8
i8 true 8
ui32 false 32
i32 true 32
ui64 false 64
i64 true 64