|
1 | 1 | # 2.8. Code Attributes |
2 | 2 |
|
3 | | -Code attributes instruct the Vala compiler details about how the code is |
4 | | -supposed to work on the target platform. Their syntax is |
5 | | -`[AttributeName]` or |
6 | | -`[AttributeName(param1 = value1, param2 = value2, ...)]`. |
7 | | - |
8 | | -They are mostly used for bindings in *vapi* files, `[CCode(...)]` being |
9 | | -the most prominent attribute here. Another example is the `[DBus(...)]` |
10 | | -attribute for exporting remote interfaces via |
11 | | -[D-Bus](http://www.freedesktop.org/wiki/Software/dbus). |
| 3 | +Code attributes instruct the Vala compiler details about how the code is supposed to work on the target platform. |
| 4 | +They are mostly used for bindings in *vapi* files. |
| 5 | + |
| 6 | +The most prominent example is the `[CCode (...)]` attribute: |
| 7 | + |
| 8 | +```vala |
| 9 | +[CCode (cname = "SDL_PropertiesID", has_type_id = false)] |
| 10 | +public struct PropertiesID : uint32 {} |
| 11 | +``` |
| 12 | + |
| 13 | +Another common example is the `[DBus (...)]` attribute, which is used for exporting remote interfaces via |
| 14 | +[D-Bus](http://www.freedesktop.org/wiki/Software/dbus): |
| 15 | + |
| 16 | +```vala |
| 17 | +[DBus (name = "net.example")] |
| 18 | +public interface Header.Example { |
| 19 | + // ... |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +Here are some examples of the syntax: |
| 24 | + |
| 25 | +```vala |
| 26 | +[AttributeName] |
| 27 | +class Example { |
| 28 | + // ... |
| 29 | +} |
| 30 | +
|
| 31 | +[AttributeName, AnotherAttributeName] |
| 32 | +var number = 1; |
| 33 | +
|
| 34 | +[AttributeName, AnotherAttributeName (name = "value"), YetAnotherAttributeName] |
| 35 | +var str = "string"; |
| 36 | +
|
| 37 | +[AttributeName (name = "value"), AnotherAttributeName] |
| 38 | +var character = 'c'; |
| 39 | +
|
| 40 | +[AttributeName] |
| 41 | +[AttributeName (name = "value")] |
| 42 | +var boolean = true; |
| 43 | +
|
| 44 | +[AttributeName (name = "value", anotherName = 1, thirdName = false)] |
| 45 | +int integer = 1; |
| 46 | +``` |
| 47 | + |
| 48 | +You can use as many attributes and arguments in the attributes as you want. |
| 49 | + |
| 50 | +For more information on attributes, refer to the [reference manual](https://gnome.pages.gitlab.gnome.org/vala/manual/attributes.html). |
0 commit comments