Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
# 2.8. Code Attributes

Code attributes instruct the Vala compiler details about how the code is
supposed to work on the target platform. Their syntax is
`[AttributeName]` or
`[AttributeName(param1 = value1, param2 = value2, ...)]`.

They are mostly used for bindings in *vapi* files, `[CCode(...)]` being
the most prominent attribute here. Another example is the `[DBus(...)]`
attribute for exporting remote interfaces via
[D-Bus](http://www.freedesktop.org/wiki/Software/dbus).
Code attributes instruct the Vala compiler details about how the code is supposed to work on the target platform.
They are mostly used for bindings in *vapi* files.

The most prominent example is the `[CCode (...)]` attribute:

```vala
[CCode (cname = "SDL_PropertiesID", has_type_id = false)]
public struct PropertiesID : uint32 {}
```

Another common example is the `[DBus (...)]` attribute, which is used for exporting remote interfaces via
[D-Bus](http://www.freedesktop.org/wiki/Software/dbus):

```vala
[DBus (name = "net.example")]
public interface Header.Example {
// ...
}
```

Here are some examples of the syntax:

```vala
[AttributeName]
class Example {
// ...
}

[AttributeName, AnotherAttributeName]
var number = 1;

[AttributeName, AnotherAttributeName (name = "value"), YetAnotherAttributeName]
var str = "string";

[AttributeName (name = "value"), AnotherAttributeName]
var character = 'c';

[AttributeName]
[AttributeName (name = "value")]
var boolean = true;

[AttributeName (name = "value", anotherName = 1, thirdName = false)]
int integer = 1;
```

You can use as many attributes and arguments in the attributes as you want.